23
March
,
2022

Quantum Computing Applications - Chemistry

Share the article
Our library

Understanding the properties of chemicals through numerical simulations has been a key enabler for chemical development. But modeling atomic interactions on classical computers quickly exceeds the computational capacity of even the largest supercomputers.

Quantum computers can help. Instead of performing ‘coarse-grained’ approximations on classical computers, scientists can perform much more precise simulations on quantum machines.

A common application for quantum chemistry is searching for the ground state energy of a molecule by finding the minimum eigenvalue of a matrix characterizing it. While this might be done in the future using quantum phase estimation, it can be performed today using much shallower circuits with the Variational Quantum Eigensolver (VQE) algorithm.

Because today’s quantum computers have limited fidelity, it is common to design hybrid quantum-classical algorithms that take advantage of the strengths of each computing paradigm. For instance, the classical computer could perform non-convex optimization and the quantum computer could efficiently calculate the target function. This hybrid approach may prove appealing to solve problems that even Density Functional Theory (DFT) or Hartree-Fock approximations can’t solve.

Here's how it looks with Classiq:

Let’s use Classiq to solve a ground state problem for the molecule H2, starting with our Python SDK. This is done by generating a Hamiltonian, creating a variational quantum circuit to calculate the Hamiltonian’s ground state, and executing said circuit in a hybrid quantum-classical VQE algorithm.

Here we define the problem as an H2 molecule, using the Classiq atom library, specifying their location, charge, and spin. This code block ends in generating the Hamiltonian.


from classiq import ModelDesigner, Executor
from classiq.builtin_functions import UCC, HartreeFock

from classiq.interface.chemistry.molecule import Molecule
from classiq.interface.chemistry.ground_state_problem import GroundStateProblem
from classiq.interface.backend.backend_preferences import IBMBackendPreferences, BackendPreferences
from classiq.interface.executor.execution_preferences import ExecutionPreferences, OptimizerPreferences
from classiq.interface.executor.hamiltonian_minimization_problem import HamiltonianMinimizationProblem
from classiq.interface.executor.quantum_instruction_set import QuantumInstructionSet
from classiq.interface.executor.quantum_program import QuantumProgram


molecule = Molecule(
    atoms=[
        ("H", (0.0, 0.0, 0.0)),
        ("H", (0.0, 0.0, 3.0)),
        ],
)
gs_problem = GroundStateProblem(
    molecule=molecule,
    mapping="jordan_wigner",
)

operator = gs_problem.generate_hamiltonian()

Now, we create an ansatz, or initial parameterized guess for our VQE algorithm, using the Hamiltonian generated. This section of code creates a hardware-efficient quantum circuit to represent the ansatz. The Classiq platform synthesizes quantum circuits in many languages, defaulting to Open QASM 2.0.


model_designer = ModelDesigner()

hf_params = HartreeFock(gs_problem=gs_problem)
output_dict = model_designer.HartreeFock(params=hf_params)
hf_output = output_dict["OUT"]

ucc_params = UCC(gs_problem=gs_problem, excitations=[1,2], max_depth=100)

model_designer.UCC(params=ucc_params, in_wires={"IN": hf_output})
circuit = model_designer.synthesize()
circuit_qasm = circuit.qasm

Last, we run the VQE algorithm, iterating 30 times, using the QASM code from the previous section as input and specifying the algorithm is executed by IBMQ's aer simulator.


optimizer_preferences = OptimizerPreferences(
    max_iteration=30
)
backend_preferences = IBMBackendPreferences(
    backend_service_provider="IBMQ", backend_name="aer_simulator"
)
num_shots = 1000
execution_preferences = ExecutionPreferences(
    num_shots=num_shots,
    backend_preferences=backend_preferences,
    optimizer_preferences=optimizer_preferences
)
hamiltonian_problem = HamiltonianMinimizationProblem(
    ansatz=QuantumProgram(code=circuit_qasm, syntax=QuantumInstructionSet.QASM),
    hamiltonian=operator
)
result = Executor(
    preferences=execution_preferences
).execute_hamiltonian_minimization(hamiltonian_problem)

print("The ground state energy of H2 is", result.energy)
print("The optimal parameters for the ansatz are", result.optimal_parameters)
result.show_convergence_graph()

The ground state energy and optimal parameters are outputted. These parameters correspond to the angles in the ansatz, and our convergence graph shows how the algorithm honed in on the correct solution.

We can do the same process with our Textual Model as well, simply running the following code.


{
    "ground_state_problem": {
        "molecule": {
            "atoms": [
                ["H", [0, 0, 0]],
                ["H", [0, 0, 3]]
            ]
        },
        "basis": "sto3g",
        "mapping": "jordan_wigner"
    },
    "quantum_circuit_constraints": {
        "logic_flow": [
            {
               "function": "HartreeFock",
               "function_params": {"gs_problem": "ground_state_problem"},
               "outputs": {"OUT": "hf_out"},
               "name": "HartreeFock"
            },
            {
                "function": "UCC",
                "function_params": {
                    "gs_problem": "ground_state_problem",
                    "excitations": [1,2],
                    "max_depth": 100
                },
                "inputs": {"IN": "hf_out"},
                "name": "UCC"
            }
        ]
    },
    "execution_preferences": {
        "optimizer_preferences": {
            "cost_type": "AVERAGE",
            "max_iteration": 30
        },
        "backend_preferences": {
            "backend_service_provider": "IBMQ",
            "backend_name": "aer_simulator"
        },
        "num_shots": 1000
    }
}

Interested in seeing what the Classiq platform can deliver for your organization? Contact us to schedule a demo

Understanding the properties of chemicals through numerical simulations has been a key enabler for chemical development. But modeling atomic interactions on classical computers quickly exceeds the computational capacity of even the largest supercomputers.

Quantum computers can help. Instead of performing ‘coarse-grained’ approximations on classical computers, scientists can perform much more precise simulations on quantum machines.

A common application for quantum chemistry is searching for the ground state energy of a molecule by finding the minimum eigenvalue of a matrix characterizing it. While this might be done in the future using quantum phase estimation, it can be performed today using much shallower circuits with the Variational Quantum Eigensolver (VQE) algorithm.

Because today’s quantum computers have limited fidelity, it is common to design hybrid quantum-classical algorithms that take advantage of the strengths of each computing paradigm. For instance, the classical computer could perform non-convex optimization and the quantum computer could efficiently calculate the target function. This hybrid approach may prove appealing to solve problems that even Density Functional Theory (DFT) or Hartree-Fock approximations can’t solve.

Here's how it looks with Classiq:

Let’s use Classiq to solve a ground state problem for the molecule H2, starting with our Python SDK. This is done by generating a Hamiltonian, creating a variational quantum circuit to calculate the Hamiltonian’s ground state, and executing said circuit in a hybrid quantum-classical VQE algorithm.

Here we define the problem as an H2 molecule, using the Classiq atom library, specifying their location, charge, and spin. This code block ends in generating the Hamiltonian.


from classiq import ModelDesigner, Executor
from classiq.builtin_functions import UCC, HartreeFock

from classiq.interface.chemistry.molecule import Molecule
from classiq.interface.chemistry.ground_state_problem import GroundStateProblem
from classiq.interface.backend.backend_preferences import IBMBackendPreferences, BackendPreferences
from classiq.interface.executor.execution_preferences import ExecutionPreferences, OptimizerPreferences
from classiq.interface.executor.hamiltonian_minimization_problem import HamiltonianMinimizationProblem
from classiq.interface.executor.quantum_instruction_set import QuantumInstructionSet
from classiq.interface.executor.quantum_program import QuantumProgram


molecule = Molecule(
    atoms=[
        ("H", (0.0, 0.0, 0.0)),
        ("H", (0.0, 0.0, 3.0)),
        ],
)
gs_problem = GroundStateProblem(
    molecule=molecule,
    mapping="jordan_wigner",
)

operator = gs_problem.generate_hamiltonian()

Now, we create an ansatz, or initial parameterized guess for our VQE algorithm, using the Hamiltonian generated. This section of code creates a hardware-efficient quantum circuit to represent the ansatz. The Classiq platform synthesizes quantum circuits in many languages, defaulting to Open QASM 2.0.


model_designer = ModelDesigner()

hf_params = HartreeFock(gs_problem=gs_problem)
output_dict = model_designer.HartreeFock(params=hf_params)
hf_output = output_dict["OUT"]

ucc_params = UCC(gs_problem=gs_problem, excitations=[1,2], max_depth=100)

model_designer.UCC(params=ucc_params, in_wires={"IN": hf_output})
circuit = model_designer.synthesize()
circuit_qasm = circuit.qasm

Last, we run the VQE algorithm, iterating 30 times, using the QASM code from the previous section as input and specifying the algorithm is executed by IBMQ's aer simulator.


optimizer_preferences = OptimizerPreferences(
    max_iteration=30
)
backend_preferences = IBMBackendPreferences(
    backend_service_provider="IBMQ", backend_name="aer_simulator"
)
num_shots = 1000
execution_preferences = ExecutionPreferences(
    num_shots=num_shots,
    backend_preferences=backend_preferences,
    optimizer_preferences=optimizer_preferences
)
hamiltonian_problem = HamiltonianMinimizationProblem(
    ansatz=QuantumProgram(code=circuit_qasm, syntax=QuantumInstructionSet.QASM),
    hamiltonian=operator
)
result = Executor(
    preferences=execution_preferences
).execute_hamiltonian_minimization(hamiltonian_problem)

print("The ground state energy of H2 is", result.energy)
print("The optimal parameters for the ansatz are", result.optimal_parameters)
result.show_convergence_graph()

The ground state energy and optimal parameters are outputted. These parameters correspond to the angles in the ansatz, and our convergence graph shows how the algorithm honed in on the correct solution.

We can do the same process with our Textual Model as well, simply running the following code.


{
    "ground_state_problem": {
        "molecule": {
            "atoms": [
                ["H", [0, 0, 0]],
                ["H", [0, 0, 3]]
            ]
        },
        "basis": "sto3g",
        "mapping": "jordan_wigner"
    },
    "quantum_circuit_constraints": {
        "logic_flow": [
            {
               "function": "HartreeFock",
               "function_params": {"gs_problem": "ground_state_problem"},
               "outputs": {"OUT": "hf_out"},
               "name": "HartreeFock"
            },
            {
                "function": "UCC",
                "function_params": {
                    "gs_problem": "ground_state_problem",
                    "excitations": [1,2],
                    "max_depth": 100
                },
                "inputs": {"IN": "hf_out"},
                "name": "UCC"
            }
        ]
    },
    "execution_preferences": {
        "optimizer_preferences": {
            "cost_type": "AVERAGE",
            "max_iteration": 30
        },
        "backend_preferences": {
            "backend_service_provider": "IBMQ",
            "backend_name": "aer_simulator"
        },
        "num_shots": 1000
    }
}

Interested in seeing what the Classiq platform can deliver for your organization? Contact us to schedule a demo

About "The Qubit Guy's Podcast"

Hosted by The Qubit Guy (Yuval Boger, our Chief Marketing Officer), the podcast hosts thought leaders in quantum computing to discuss business and technical questions that impact the quantum computing ecosystem. Our guests provide interesting insights about quantum computer software and algorithm, quantum computer hardware, key applications for quantum computing, market studies of the quantum industry and more.

If you would like to suggest a guest for the podcast, please contact us.

量子ソフトウェア開発を開始

お問い合わせ