A Simple Example
Step 1> Obtain a valid API key for the channel#
Example: 9B41FB6534584E45BD6BE0E6
Users need to obtain the api_key from Origin Quantum cloud management platform
Users need to contact Origin Quantum cloud administrator to grant permissions for the existing api_key
Step 2> Login#
Login to the access channel of the computing backend using the information in config.yml
# python code
# Continue from the previous example on this page
from qpanda3_runtime import RuntimeService
# Login using the data in config.yml
# Login is performed when generating the RuntimeService object
service = RuntimeService()
service.login('your_valid_api_key')
Step 3> Define and Submit Quantum Computing Tasks#
Supports two basic quantum computing tasks
Supports real quantum computer/FakeBackend as quantum computing devices
Real quantum computer/chip devices
Supports submitting computing tasks through session mechanism
Meets scenarios requiring continuous execution of user computing tasks
Generic session
Session for variational quantum computing
Here we demonstrate using expectation estimation task as an example
The quantum circuit (corresponding to the prog variable in the code below) supports input as QProg/QCircuit/OriginIR
The observable (corresponding to the ham variable in the code below) supports input as Hamiltonian/Pauli Operator/Hamiltonian string in the format supported by Origin Quantum Cloud
Use
RuntimeService.device(chip_id)to specify the quantum computer chip (corresponding todevice=service.device('WK_C180')in the example code below). The chip ID chip_id can be obtained by referring to List Supported QPUs (corresponding to the ‘id’ field), or by querying the Origin Quantum Cloud management platform.
# python code
# Continue from the previous example on this page
from pyqpanda3.core import *
from pyqpanda3.hamiltonian import Hamiltonian
# Design/Define quantum circuit
prog = QProg()
prog << X(0)<<CNOT(0,1)
ham = Hamiltonian([('XZ', [0, 1], 3.14),('XZ', [0, 1], 1.14), ('ZY', [0, 1], 5.14)])
# Use RuntimeService.estimate to generate a local management object task for the computing task
task = service.estimate(
circuit_with_observable=(prog,ham),
# For QPU, use the RuntimeService.device interface to return the device object, the parameter is the QPU id
device=service.device('WK_C180'),
)
Note: For expectation estimation tasks, the measurement qubits will automatically be consistent with the qubits in the observable (Hamiltonian)
Step 4> Query Computing Task Execution Results#
For detailed introduction on querying computing task execution results, please refer to Query Computing Results
Here we use the synchronous interface for demonstration
# python code
# Continue from the previous example on this page
res = task.get_result_sync()
print(f'res:{res}')
Complete Example Code#
# python code
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
from pyqpanda3.hamiltonian import Hamiltonian
# Login using the data in config.yml
# Login is performed when generating the RuntimeService object
service = RuntimeService()
service.login('your_valid_api_key')
# Design/Define quantum circuit
prog = QProg()
prog << X(0)<<CNOT(0,1)
ham = Hamiltonian([('XZ', [0, 1], 3.14),('XZ', [0, 1], 1.14), ('ZY', [0, 1], 5.14)])
# Use RuntimeService.estimate to generate a local management object task for the computing task
task = service.estimate(
circuit_with_observable=(prog,ham),
# For QPU, use the RuntimeService.device interface to return the device object, the parameter is the QPU id
device=service.device('WK_C180'),
)
res = task.get_result_sync()
print(f'res:{res}')