Quickstart¶
Installation¶
Quantum computing features are provided as an add-on to the Amplify SDK. To use them, install the package with the extra dependencies using the following command.
$ python3 -m pip install -U 'amplify[extra]'
Once installed correctly, you can use the quantum computing features as follows.
>>> from amplify import QAOA
If the extra packages are not installed, an error like the following is displayed.
ImportError: To enable quantum functions, install the required extras with: `python3 -m pip install -U 'amplify[extra]'`
The following environments have been verified.
3.10
3.11
3.12
3.13
Windows 10/11
Linux
Ubuntu 22.04/24.04, Rocky Linux 9.6/10.0
x86_64
macOS
ARM64 (Monterey or later)
Installation of dependency packages may fail depending on your Python version and OS.
Caution
Unlike the verified environments for the Amplify SDK, the following environments may not work.
3.14
Linux
Ubuntu 26.04
ARM64
macOS
x86_64 (Monterey or later)
Solving with Quantum Computers¶
Caution
For solving with quantum annealing machines, see D-Wave Systems.
You can solve combinatorial optimization problems using various quantum computers offered as cloud services, as well as local simulators. Here, we use the Qiskit Aer Simulator — a local simulator — to execute the quantum circuit, together with QAOA as the quantum optimization algorithm.
1. Creating the combinatorial optimization model¶
Consider the following sample problem. For an explanation of how to formulate problems with the Amplify SDK, see the Amplify SDK Quickstart.
- The objective function
- \[ \text{minimize:} \quad f = q_0 q_1 + q_0 - q_1 + 1 \]
- The decision variables
- \[ q_0, q_1 \in \{0, 1\} \]
- The constraints
- \[ \text{None} \]
>>> from amplify import Model, VariableGenerator
>>> g = VariableGenerator() # Create a generator for decision variables
>>> q = g.array("Binary", 2) # Define the decision variables
>>> f = q[0] * q[1] + q[0] - q[1] + 1 # Define the objective function
>>> model = Model(f) # Create the model
2. Creating the solver client¶
To run optimization with QAOA on the Qiskit Aer Simulator, we create the solver client using the Qiskit Aer Simulator client class (AerClient) together with the QAOA algorithm class (QAOA).
>>> from amplify import QAOA, AerClient
>>> client = AerClient(QAOA) # Combine QAOA with the Qiskit Aer Simulator
We also set the QAOA circuit depth to 2 and the number of measurements per optimization step to 1000.
>>> client.parameters.reps = 2 # Set the QAOA ansatz circuit depth to 2
>>> client.parameters.shots = 1000 # Set the number of measurements per optimization step to 1000
As with other clients, run the solver via the solve() function to perform optimization with QAOA.
>>> from amplify import solve
>>> result = solve(model, client) # Run QAOA
3. Checking the results¶
The result is returned as an instance of the Result class. This instance contains the result of the solver run and information about the model conversions performed. Calling the best property returns the best of the solutions obtained. For details on retrieving results, see 7. Solving combinatorial optimization problems - Retrieving the result.
>>> result.best.objective # Value of the objective function
0.0
>>> result.best.values # Values of the variables
Values({Poly(q_0): 0, Poly(q_1): 1})
- Retrieving the quantum circuit:
The quantum circuit executed at each classical optimization step can be obtained via history.
>>> # Display the quantum circuit used for sampling
>>> print(result.client_result.history[0].sampling_meta.circuit)
...
The type of the circuit object depends on the backend. With AerClient, it is a qiskit.circuit.QuantumCircuit. For details, see each client’s page.
- Retrieving the final measurement results:
The results of the final measurement performed with the optimized parameters \(\boldsymbol{\theta}^{\textup{opt}}\) can be obtained via optimized_counts. Each element is a tuple of “Ising sequence (\(\{-1, 1\}\) valued) and count”; Ising sequences with higher counts are stronger candidates for the optimal solution.
>>> # Display measurement results at the optimal parameters, sorted by count (descending)
>>> for ising_seq, freq in sorted(result.client_result.optimized_counts, key=lambda x: x[1], reverse=True):
>>> print(f"Ising sequence: {ising_seq}, Count: {freq}")
For how to convert Ising sequences back to the original decision variable array and how to interpret the distribution, see Measurement Results at Best Parameters.
Next steps¶
For details on connecting to quantum computers, see Quantum computers.
For solving with each algorithm and retrieving detailed results, see QAOA and Recursive QAOA.