Sampling Task#
A Simple Example#
Step 1> Login#
# python code
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
service = RuntimeService()
service.login('your_valid_api_key')
Step 2> Define the quantum circuit for sampling#
# python code
# Continue from the previous example on this page
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
Step 3> Select the device to execute the task#
# python code
# Continue from the previous example on this page
dev=service.device('WK_C180') # Here we use the quantum computing chip with id WK_C180
Step 4> Submit the task to the computing backend#
# python code
# Continue from the previous example on this page
task = service.sample(
prog,
dev,
)
Step 5> Query the task execution result#
# 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 *
service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
dev=service.device('WK_C180') # Here we use the quantum computing chip with id WK_C180
task = service.sample(
prog,
dev,
)
res = task.get_result_sync()
print(f'res:{res}')
Single Quantum Circuit Computing Task Example#
# python code
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
task = service.sample(
prog,
service.device('WK_C180') # Here we use the quantum computing chip with id WK_C180
)
res = task.get_result_sync() # Query the task computing result
print(f'res:{res}')
Batch Sampling Example for Multiple Quantum Circuits#
# python code
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
prog2 = QProg()
prog2 << Y(1)<<CZ(1,0)<<measure(0,0)<<measure(1,1)
prog3 = QProg()
prog3 << Z(0)<<SWAP(0,1)<<measure(0,0)<<measure(1,1)
task = service.sample(
[prog,prog2,prog3], # Pass a list of quantum circuits for batch sampling
service.device('WK_C180') # Here we use the quantum computing chip with id WK_C180
)
res = task.get_result_sync() # Query the task computing result
print(f'res:{res}')
Ways to Pass Quantum Circuits#
Supports passing quantum circuits as QProg/QCircuit/OriginIR
In the example code below, we define and submit a batch sampling task with three circuits
The initial forms of the three quantum circuits are QProg object, QCircuit object, and OriginIR string respectively (This usage is not common, it’s only to demonstrate the related functionality)
Special attention needed:
QCircuit object cannot carry measurement qubits
Sampling task requires users to specify measurement qubits
Either carried in the quantum circuit (QProg/OriginIR)
Or specified through the measure_qubits parameter of RuntimeService.sample.
Therefore, in the code below, measure_qubits must be used to specify the measurement qubits
# python code
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0)<<CNOT(0,1)
cir = QCircuit()
cir << Y(1)<<CZ(1,0)
ir_str = '''
QINIT 2
CREG 2
Y q[1]
CZ q[1],q[0]
'''
task = service.sample(
[prog,cir,ir_str], # Pass a list of quantum circuits for batch sampling
service.device('WK_C180'), # Here we use the quantum computing chip with id WK_C180
measure_qubits=[0,1]
)
res = task.get_result_sync() # Query the task computing result
print(f'res:{res}')
Specify Measurement Qubits#
Method 1> Carried by the quantum circuit itself#
As shown in the code below
Both QProg and OriginIR can carry measurement qubits
Special attention: In this case, measure_qubits cannot be used to specify qubits, otherwise it will be overwritten
In batch circuit sampling tasks, the measurement qubits of each quantum circuit can be different
# python code
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
ir_str = '''
QINIT 2
CREG 2
X q[1]
CNOT q[1],q[0]
MEASURE q[1],c[1]
'''
task = service.sample(
[prog,ir_str], # Pass a list of quantum circuits for batch sampling
service.device('WK_C180'), # Here we use the quantum computing chip with id WK_C180
shots=1000
)
res = task.get_result_sync() # Query the task computing result
print(f'res:{res}')
A sampling result
res:[[{'key': ['0x0', '0x1', '0x2', '0x3'], 'value': [0.002, 0.036, 0.044, 0.918]}, {'key': ['0x0', '0x1'], 'value': [0.041, 0.959]}]]
Simple analysis of sampling results
The dictionary
{'key': ['0x0', '0x1', '0x2', '0x3'], 'value': [0.002, 0.036, 0.044, 0.918]}corresponds to the first quantum circuitThe initial form of the first quantum circuit is QProg object
The measurement qubit indices of the first quantum circuit are [0,1], corresponding to basis states |00>,|01>,|10>,|11>
00,01,10,11 written in hexadecimal: 0x0,0x1,0x2,0x3
The default endianness: the left side corresponds to the qubit with larger index, e.g., |01> means qubit 1 is 0, qubit 0 is 1
In the sampling result above, all 4 basis states appeared, and no other basis states appeared, which is consistent with the setting of measuring two qubits
The theoretical final state of the first quantum circuit is |11>, so in the sampling result, the probability of 0x3 should be the highest and close to 100%
In the sampling result above, the probability of 0x3 is the largest, close to 1, which is consistent with the theoretical value
The dictionary
{'key': ['0x0', '0x1'], 'value': [0.041, 0.959]}corresponds to the second quantum circuitThe initial form of the second quantum circuit is OriginIR string
The measurement qubit index of the second quantum circuit is [1], corresponding to basis states |0>,|1>
0,1 written in hexadecimal: 0x0,0x1
In the sampling result above, both basis states appeared, and no basis states outside {|0>,|1>} appeared, which is consistent with the setting of measuring 1 qubit
The theoretical final state of the second quantum circuit is |11>, only taking the value of qubit 1, which should be |1>, so in the sampling result, the probability of 0x1 should be the highest and close to 100%
In the sampling result above, the probability of 0x1 is the largest, close to 1, which is consistent with the theoretical value
Method 2> Specify/Update through measure_qubits parameter#
Overwrite feature: If there are measurement nodes in the original quantum circuit, the measurement qubit indices in measure_qubits will replace the measurement qubits in the circuit
In the example code below, the original measurement qubits of both circuits are cleared and replaced with qubit 0
Preservation feature: measure_qubits cannot contain qubit indices that do not exist in the original quantum circuit
In the example code below, if measure_qubits is assigned [2], an error will be reported
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
ir_str = '''
QINIT 2
CREG 2
X q[1]
CNOT q[1],q[0]
MEASURE q[1],c[1]
'''
task = service.sample(
[prog,ir_str], # Pass a list of quantum circuits for batch sampling
service.device('WK_C180'), # Here we use the quantum computing chip with id WK_C180
shots=1000,
measure_qubits=[0]
)
res = task.get_result_sync() # Query the task computing result
print(f'res:{res}')
A sampling result
res:[[{'key': ['0x0', '0x1'], 'value': [0.046, 0.954]}, {'key': ['0x0', '0x1'], 'value': [0.043, 0.957]}]]
Sampling Using FakeBackend#
For detailed information, please refer to FakeBackend
Step 1> Login#
# python code
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
service = RuntimeService()
service.login('your_valid_api_key')
Step 2> Define the quantum circuit for sampling#
# python code
# Continue from the previous example on this page
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
Step 3> Select the device to execute the task#
In the example code below, dev is a limited simulation of the real quantum computing chip with id WK_C180, including:
Has the same qubit topology structure
Uses limited noise simulation with noise data (T1, T2, readout errors, etc.) from real quantum chip device specifications
# python code
# Continue from the previous example on this page
dev=service.device('WK_C180').fake_backend() # Here we use the Fake Backend corresponding to the quantum computing chip with id WK_C180
Step 4> Submit the task to Fake Backend#
When the backend device is Fake Backend,
RuntimeService.samplemust run in the__name__ == '__main__'environmentThe sampling calculation of Fake Backend is computed locally on the client
# python code
# Continue from the previous example on this page
task = service.sample(
prog,
dev,
)
Step 5> Query the task execution result#
# python code
# Continue from the previous example on this page
res = task.get_result_sync()
print(f'res:{res}')
Step 6> Run On QPU#
If needed Submit the computing task data verified through Fake Backend to the corresponding real device for execution
# python code
# Continue from the previous example on this page
task2 = task.run_on_real_device(shots=1000)
res2 = task2.get_result_sync()
print(f'res2:',res2)
Complete Example Code#
# python code
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *
def est():
service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
dev=service.device('WK_C180').fake_backend() # Here we use the Fake Backend corresponding to the quantum computing chip with id WK_C180
task = service.sample(
prog,
dev,
)
res = task.get_result_sync()
print(f'res:{res}')
task2 = task.run_on_real_device(shots=1000)
res2 = task2.get_result_sync()
print(f'res2:',res2)
if __name__ == '__main__':
est()