Quantum ComputersΒΆ
Clients for quantum computers run optimization with quantum algorithms such as QAOA.
They execute quantum circuits using various quantum computers available as cloud services, as well as local simulators.
Tip
Clients that use local simulators can be used without any registration.
For other clients, you need a contract with the respective quantum computing service providers.
Hint
To use quantum computers, install Amplify with extra packages using the following command:
$ python3 -m pip install -U 'amplify[extra]'
Caution
Some extra packages may not be available depending on your Python version. See Supported environments for details.
The setup steps vary depending on the client.
Clients that use cloud APIs require authentication with the license you hold.
Client class |
Type |
Required setup |
|---|---|---|
π» Local |
None |
|
π» Local |
None |
|
π» Local |
None |
|
βοΈ Cloud |
An IBM Quantum API key obtained from your IBM Cloud account. |
|
βοΈ Cloud |
Authentication with an AWS account. |
Quantum computer clients also require specifying an algorithm at initialization.
The variable types and polynomial degree accepted for the input problem depend on the chosen algorithm.
When QAOA is specified as the client argument
Binary
Ising
Integer
Real
Objective function
-
Nth degree*
-
-
Equality constraint
-
**
-
-
Inequality constraint
-
-
-
-
*: Problems of arbitrary degree are supported. However, depending on the qubit connectivity of the quantum computer, the required number of qubits may increase.
**: When Constrained QAOA is selected via QAOA type, N-HOT constraints are supported.
When RQAOA is specified as the client argument
Binary
Ising
Integer
Real
Objective function
-
Nth degree*
-
-
Equality constraint
-
-
-
-
Inequality constraint
-
-
-
-
*: Problems of arbitrary degree are supported. However, depending on the qubit connectivity of the quantum computer, the required number of qubits may increase.
Quantum computer clients support setting parameters specific to each quantum computer/simulator, in addition to the common client class interface.
Clients configured with the same algorithm expose the same parameters via client.parameters. For details on parameters and result information, see Algorithm details.
- Configuration example:
The following example creates a client that runs QAOA using the Qulacs Simulator.
from amplify import QAOA, QulacsClient # Create a solver client by combining the algorithm class QAOA with the backend class QulacsClient client = QulacsClient(QAOA) # Set QAOA parameters: reps and shots client.parameters.reps = 1 client.parameters.shots = 100
- Execution example:
from amplify import Model, VariableGenerator, solve # Create decision variables and the objective function g = VariableGenerator() q = g.array("Binary", 2) f = q[0] * q[1] + q[0] - q[1] + 1 # Create a model model = Model(f) # Run the solver result = solve(model, client)
Obtain the backend version:
>>> client.version() '0.6.13'
Obtain the execution time:
>>> d = result.client_result.durations >>> result.total_time # Total time spent in amplify.solve datetime.timedelta(microseconds=26152) >>> d.total_time # Total elapsed time for QAOA datetime.timedelta(microseconds=25779) >>> d.total_response_time # Total communication time with the QPU datetime.timedelta(microseconds=283) >>> d.total_execution_time # Total execution time on the QPU datetime.timedelta(microseconds=283) >>> d.classical_processing_time # Time spent on classical optimization (= total_time - total_response_time) datetime.timedelta(microseconds=25496)
Obtaining the quantum circuit:
>>> print(result.client_result.history[0].sampling_meta.circuit)
The type of the circuit object depends on the backend.
Interpreting sampling results:
>>> sorted_counts = sorted(result.client_result.optimized_counts, key=lambda x: x[1], reverse=True) >>> for sol, freq in sorted_counts[:5]: ... values = q.substitute( ... { ... k: p.substitute( ... {v: sol[v.id] for v in result.intermediate.model.get_variables()} ... ) ... for k, p in result.intermediate.mapping.items() ... } ... ) ... print(f"Solution: {values}, Count: {freq}") Solution: [0, 1], Count: 76 Solution: [0, 0], Count: 17 Solution: [1, 0], Count: 4 Solution: [1, 1], Count: 3