Appendix E:cqlib Tutorial¶
1. Quantum Computer Operation Basics¶
Currently supports two platforms for quantum computer operations:
-
GuoDunPlatform
GuoDun Quantum Computing Cloud Platform, official website: https://quantumctek-cloud.com/ -
TianYanPlatform
China Telecom TianYan Quantum Computing Cloud Platform, official website: https://qc.zdxlz.com
Before using, please register an account on the corresponding platform and obtain the corresponding key.
Before starting this tutorial, please ensure that the cqlib package has been successfully installed. If not installed, please run the following command:
!pip install cqlib -i https://pypi.tuna.tsinghua.edu.cn/simple
Set login information and select a quantum computer or simulator.
from cqlib import TianYanPlatform # Import quantum computer SDK support package
login_key = "your_login_key"
# Use the TianYanPlatform class to create a platform instance, and set your SDK key and chosen quantum computer.
# When users use this, please be sure to update with your own key, otherwise the above key will be updated regularly and the example program will not work.
platform = TianYanPlatform(login_key=login_key)
# Query and select quantum computer, can be used later to analyze quantum computer working status. The name for calling the quantum computer can be viewed on the webpage.
computer_list_data = platform.query_quantum_computer_list()
for computer_data in computer_list_data:
print(computer_data)
# This is an example, please select the correct quantum computer or simulator in actual use.
platform.set_machine("xuanyuanone")
1 2 3 4 5 6 |
|
You can also specify the quantum computer directly when creating the instance. The following instruction is equivalent to the above:
platform = TianYanPlatform(login_key=login_key, machine_name="xuanyuanone")
First, we queried the list of quantum computers included in the current cloud platform, and set which quantum computer to use according to requirements. Next, we'll introduce quantum circuit writing and submission.
2. Writing Quantum Circuits¶
2.1 Quantum Gates¶
Cqlib supports a rich variety of quantum gates, including many single-qubit gates and multi-qubit gates
-
Single-qubit gates:
- Hadamard gate: H
- Pauli gates: X, Y, Z
- Rotation gates: RX, RY, RZ
- Root Pauli gates: X2P, X2M, Y2P, Y2M
- Phase gates: S, SD, T, TD
-
Multi-qubit gates:
- Controlled gates: CX, CCX, CRX, CRY, CRZ, CZ
- Swap gate: SWAP
- XY gates: XY, XY2P, XY2M
These gates are the foundation for building quantum circuits and can be combined to perform complex quantum computing tasks.
2.2 Quantum Program Writing¶
Below introduces the first quantum program: Bell state preparation.
Bell states are important entangled states in quantum mechanics, involving two qubits. Bell states have important applications in quantum information and quantum computing.
The Bell state preparation process uses quantum gate operations (Hadamard gate and CZ gate) to generate a Bell state, and measures the qubits (M).
from cqlib.circuits import Circuit, Parameter
circuit = Circuit(qubits=[0, 6])
circuit.h(0)
circuit.x(6)
# cx gate is decomposed into h and cz gates
circuit.h(6)
circuit.cz(0, 6)
circuit.h(6)
circuit.measure_all()
print(circuit.qcis)
1 2 3 4 5 6 7 |
|
Parameterized quantum circuits are a commonly used technique in quantum computing that allows introducing parameters into quantum circuits to increase flexibility and adjustability. This is very useful in quantum machine learning, optimization algorithms, and quantum chemistry.
theta = Parameter('theta')
circuit_para = Circuit(qubits=[0], parameters=[theta])
circuit_para.rx(0, theta)
print(f"Circuit with parameters: {circuit_para.qcis}")
circuit_para.set_parameter_value({'theta': 0.12})
print(f"Circuit after assignment: {circuit_para.qcis}")
1 2 |
|
Quantum computing parameters support basic arithmetic operations: addition, subtraction, multiplication, and division.
from cqlib.circuits import Circuit, Parameter
theta = Parameter('theta')
phi = Parameter('phi')
print(f'theta: {theta}')
print(f'phi: {phi}')
print(f"theta + phi: {theta + phi}")
print(f"theta - phi: {theta - phi}")
print(f"theta * phi: {theta * phi}")
print(f"theta / phi: {theta / phi}")
# Combined operations
print(f"theta * phi - phi: {theta * phi - phi}")
1 2 3 4 5 6 7 |
|
circuit.qcis
can export QCIS circuits. Circuit.load
can import QCIS circuits to create a quantum circuit.
qcis = """
H Q0
X Q6
H Q6
CZ Q0 Q6
H Q6
M Q0
M Q6
"""
c1 = Circuit.load(qcis)
print(c1)
1 |
|
3. Quantum Computing Submission¶
3.1 Quantum Experiment Collection Creation¶
Quantum experiment collections can help organize similar experiments together for easy management and classification.
If you don't create an experiment collection (just don't pass the lab_id parameter later), quickly submitted programs will be placed in the default experiment collection.
from datetime import datetime
# The create_lab method creates an experiment collection and returns the unique identifier lab_id for that collection
lab_id = platform.create_lab(name=f'lab.{datetime.now().strftime("%Y%m%d%H%M%S")}', remark='test_collection')
print(lab_id)
1 |
|
3.2 Quantum Experiment Run Submission¶
There are two ways to submit quantum circuit experiments: + Save the experiment first, then submit for execution. + Directly submit and run the experiment.
These are introduced separately below:
3.2.1 Save Experiment, Then Submit¶
In quantum computing, typically you first define a quantum circuit (like the previously defined circuit.qcis), then save it as an experiment and submit it for execution. Here we use the save_experiment method to save the experiment, then use the run_experiment method to submit for execution, specifying the number of runs (5000 times).
The advantage of this method is that you can store one circuit and run it multiple times without recreating the experiment.
exp_id = platform.save_experiment(lab_id=lab_id, circuit=circuit.qcis, name=f'exp.{datetime.now().strftime("%Y%m%d%H%M%S")}')
query_id_single = platform.run_experiment(exp_id=exp_id, num_shots=5000)
print(f'query_id: {query_id_single}')
1 |
|
exp_id is the unique identifier of the saved experiment, and query_id_single is the query identifier returned after submission, which can be used for subsequent status queries.
3.2.2 Directly Submit and Run Experiment¶
If you don't need to save the experiment first, you can also directly submit and run the experiment. Using the submit_experiment method can accomplish this in one step, directly submitting the quantum circuit to the specified experiment collection and running it.
query_id_single = platform.submit_job(
circuit=circuit.qcis,
exp_name=f'exp.{datetime.now().strftime("%Y%m%d%H%M%S")}',
lab_id=lab_id,
num_shots=5000,
)
print(f'query_id: {query_id_single}')
1 |
|
3.2.3 Batch Submit and Run Experiments¶
If you have multiple quantum circuits to run, you can pass them as a list to the submit_experiment method for batch submission and execution. This batch experiment submission is mainly for cases where user circuits are basically the same, especially when measurement qubits are the same, and only the input parameters are different.
circuit_list = [circuit.qcis] * 10
# Users can reorganize this list according to actual needs, but note that measurement qubits and measurement order must be the same.
# For other limitations of batch running experiments, see the function description.
from cqlib import QuantumLanguage
query_id_list = platform.submit_experiment(
circuit=circuit_list,
language=QuantumLanguage.QCIS,
lab_id=lab_id,
num_shots=5000,
)
print(f'Submitted multiple circuits query_id: {query_id_list}')
1 |
|
4. Querying Experimental Results and Processing¶
4.1 Querying Raw Data¶
Once the experiment is submitted and processed by the quantum computing platform, you can use the returned query_id_list to query the results of each experiment. Below is example code for querying the results of a single experiment:
exp_result = platform.query_experiment(query_id=query_id_single, max_wait_time=120, sleep_time=5)
# Return value is a list containing several dictionary forms,
# key: "resultStatus" is the raw data of circuit execution, totaling 1+num_shots data points, the first data point is the measured qubit numbers and order, such as [0, 6] in this example, the rest are results for each shot, with each shot result arranged according to qubit order.
# key: "probability" is the probability statistics of circuit measurement results, statistical results after real-time readout correction.
# key: "experimentTaskId" is the query id for this experiment, mainly used for result correspondence confirmation in batch experiments.
# When measurement qubits are greater than 15, result statistics require high server resources and large data transmission rates, so "probability" returns empty. Please use raw data combined with the quantum computer's readout fidelity at the time to make corrections yourself. Related correction functions are shown in advanced tutorials. Users can also improve correction functions themselves.
for res_name, res_data in exp_result[0].items():
print(f"{res_name} : {res_data}")
1 2 3 |
|
For multiple submitted quantum circuits, or single quantum circuits with measurement qubits greater than or equal to 15 qubits, the cloud platform will only return raw measurement results.
You can also batch query quantum circuit results.
exp_result = platform.query_experiment(query_id=query_id_list, max_wait_time=120, sleep_time=5)
print(f'Number of input query IDs: {len(query_id_list)}, Number of queried experiment results: {len(exp_result)}')
print('First quantum circuit experiment result:')
for res_name, res_data in exp_result[0].items():
print(f"{res_name} : {res_data}")
1 2 3 4 5 |
|
4.2 Experimental Result Statistics¶
For experiments where the cloud platform doesn't return statistical results, you need to manually perform statistics on the experimental results. You need to use the LaboratoryUtils class from the util module. Where: readout_data_to_state_probabilities_whole method will count all possible spaces, outputting space combinations with zero probability. readout_data_to_state_probabilities_part only outputs results with probability greater than 0, helping to save memory.
from cqlib.utils import LaboratoryUtils
lu = LaboratoryUtils()
# Count all spaces of results
probability_whole=lu.readout_data_to_state_probabilities_whole(result=exp_result[0])
print(f'Complete space statistics of results: {probability_whole}')
# Only count existing results, results with zero probability will not appear.
probability_part=lu.readout_data_to_state_probabilities_part(result=exp_result[0])
print(f'Partial space statistics of results: {probability_part}')
1 2 |
|
4.3 Readout Correction and Normalization Processing (Not needed for simulators)¶
You can perform probability calibration and probability correction based on the experimental results read.
single_circuit_result = exp_result[0]
calibration_result = lu.probability_calibration(result=exp_result[0], laboratory=platform)
print(f'Probability calibration result: {calibration_result}')
corrected_result = lu.probability_correction(probabilities=calibration_result)
print(f'Corrected probability result: {corrected_result}')
1 2 |
|
4.4 Quantum Computer Information Storage (Not needed for simulators)¶
If you need to save the quantum computing parameters at this time for future reference, you can run the following command:
config_save = platform.download_config()
for key, value in config_save.items():
print(key, value)
calibrationTime 2024-08-08 21:31:50 computerId QubitPrime disabledCouplers G10,G103,G29,G38,G19,G5,G42,G28,G64,G32,G75,G76,G53,G25,G54,G21,G86,G31,G40,G30,G65,G104,G20,G87 disabledQubits Q47,Q35,Q5 overview {'name': '', 'type': '', 'qubits': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q35', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q47', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'couplers': ['G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10', 'G11', 'G12', 'G13', 'G14', 'G15', 'G16', 'G17', 'G18', 'G19', 'G20', 'G21', 'G22', 'G23', 'G24', 'G25', 'G26', 'G27', 'G28', 'G29', 'G30', 'G31', 'G32', 'G33', 'G34', 'G35', 'G36', 'G37', 'G38', 'G39', 'G40', 'G41', 'G42', 'G43', 'G44', 'G45', 'G46', 'G47', 'G48', 'G49', 'G50', 'G51', 'G52', 'G53', 'G54', 'G55', 'G56', 'G57', 'G58', 'G59', 'G60', 'G61', 'G62', 'G63', 'G64', 'G65', 'G66', 'G67', 'G68', 'G69', 'G70', 'G71', 'G72', 'G73', 'G74', 'G75', 'G76', 'G77', 'G78', 'G79', 'G80', 'G81', 'G82', 'G83', 'G84', 'G85', 'G86', 'G87', 'G88', 'G89', 'G90', 'G91', 'G92', 'G93', 'G94', 'G95', 'G96', 'G97', 'G98', 'G99', 'G100', 'G101', 'G102', 'G103', 'G104', 'G105', 'G106', 'G107', 'G108', 'G109'], 'readouts': ['R01', 'R02', 'R03', 'R04', 'R05', 'R06', 'R07', 'R08', 'R09', 'R10', 'R11'], 'coupler_map': {'G0': ['Q6', 'Q0'], 'G1': ['Q7', 'Q0'], 'G2': ['Q7', 'Q1'], 'G3': ['Q8', 'Q1'], 'G4': ['Q8', 'Q2'], 'G5': ['Q9', 'Q2'], 'G6': ['Q9', 'Q3'], 'G7': ['Q10', 'Q3'], 'G8': ['Q10', 'Q4'], 'G9': ['Q11', 'Q4'], 'G10': ['Q11', 'Q5'], 'G11': ['Q12', 'Q6'], 'G12': ['Q12', 'Q7'], 'G13': ['Q13', 'Q7'], 'G14': ['Q13', 'Q8'], 'G15': ['Q14', 'Q8'], 'G16': ['Q14', 'Q9'], 'G17': ['Q15', 'Q9'], 'G18': ['Q15', 'Q10'], 'G19': ['Q16', 'Q10'], 'G20': ['Q16', 'Q11'], 'G21': ['Q17', 'Q11'], 'G22': ['Q18', 'Q12'], 'G23': ['Q19', 'Q12'], 'G24': ['Q19', 'Q13'], 'G25': ['Q20', 'Q13'], 'G26': ['Q20', 'Q14'], 'G27': ['Q21', 'Q14'], 'G28': ['Q21', 'Q15'], 'G29': ['Q22', 'Q15'], 'G30': ['Q22', 'Q16'], 'G31': ['Q23', 'Q16'], 'G32': ['Q23', 'Q17'], 'G33': ['Q24', 'Q18'], 'G34': ['Q24', 'Q19'], 'G35': ['Q25', 'Q19'], 'G36': ['Q25', 'Q20'], 'G37': ['Q26', 'Q20'], 'G38': ['Q26', 'Q21'], 'G39': ['Q27', 'Q21'], 'G40': ['Q27', 'Q22'], 'G41': ['Q28', 'Q22'], 'G42': ['Q28', 'Q23'], 'G43': ['Q29', 'Q23'], 'G44': ['Q30', 'Q24'], 'G45': ['Q31', 'Q24'], 'G46': ['Q31', 'Q25'], 'G47': ['Q32', 'Q25'], 'G48': ['Q32', 'Q26'], 'G49': ['Q33', 'Q26'], 'G50': ['Q33', 'Q27'], 'G51': ['Q34', 'Q27'], 'G52': ['Q34', 'Q28'], 'G53': ['Q35', 'Q28'], 'G54': ['Q35', 'Q29'], 'G55': ['Q36', 'Q30'], 'G56': ['Q36', 'Q31'], 'G57': ['Q37', 'Q31'], 'G58': ['Q37', 'Q32'], 'G59': ['Q38', 'Q32'], 'G60': ['Q38', 'Q33'], 'G61': ['Q39', 'Q33'], 'G62': ['Q39', 'Q34'], 'G63': ['Q40', 'Q34'], 'G64': ['Q40', 'Q35'], 'G65': ['Q41', 'Q35'], 'G66': ['Q42', 'Q36'], 'G67': ['Q43', 'Q36'], 'G68': ['Q43', 'Q37'], 'G69': ['Q44', 'Q37'], 'G70': ['Q44', 'Q38'], 'G71': ['Q45', 'Q38'], 'G72': ['Q45', 'Q39'], 'G73': ['Q46', 'Q39'], 'G74': ['Q46', 'Q40'], 'G75': ['Q47', 'Q40'], 'G76': ['Q47', 'Q41'], 'G77': ['Q48', 'Q42'], 'G78': ['Q48', 'Q43'], 'G79': ['Q49', 'Q43'], 'G80': ['Q49', 'Q44'], 'G81': ['Q50', 'Q44'], 'G82': ['Q50', 'Q45'], 'G83': ['Q51', 'Q45'], 'G84': ['Q51', 'Q46'], 'G85': ['Q52', 'Q46'], 'G86': ['Q52', 'Q47'], 'G87': ['Q53', 'Q47'], 'G88': ['Q54', 'Q48'], 'G89': ['Q55', 'Q48'], 'G90': ['Q55', 'Q49'], 'G91': ['Q56', 'Q49'], 'G92': ['Q56', 'Q50'], 'G93': ['Q57', 'Q50'], 'G94': ['Q57', 'Q51'], 'G95': ['Q58', 'Q51'], 'G96': ['Q58', 'Q52'], 'G97': ['Q59', 'Q52'], 'G98': ['Q59', 'Q53'], 'G99': ['Q60', 'Q54'], 'G100': ['Q60', 'Q55'], 'G101': ['Q61', 'Q55'], 'G102': ['Q61', 'Q56'], 'G103': ['Q62', 'Q56'], 'G104': ['Q62', 'Q57'], 'G105': ['Q63', 'Q57'], 'G106': ['Q63', 'Q58'], 'G107': ['Q64', 'Q58'], 'G108': ['Q64', 'Q59'], 'G109': ['Q65', 'Q59']}, 'qubits_length': 66, 'couplers_length': 110, 'readouts_length': 11, 'T1': 33.88, 'T2': 3.07, 'cz_error': 1.95, '1q_gate_error': 0.63, 'readout_error': 5.8} qubit {'frequency': {'f01': {'param_list': [4.8324, 4.8938, 4.8009, 4.5534, 4.8471, 4.9181, 4.7476, 4.6197, 4.8984, 4.6354, 4.5766, 5.014, 4.7793, 4.7354, 4.8115, 4.7592, 4.6921, 4.7196, 4.8209, 4.8553, 4.929, 4.7923, 4.5555, 4.8881, 4.7144, 4.5223, 4.8424, 4.6854, 4.6491, 4.7262, 4.5657, 4.597, 4.6419, 4.7488, 4.8926, 4.9018, 4.7643, 4.6821, 4.5529, 4.6739, 4.7315, 4.7362, 4.6938, 4.9484, 4.6206, 4.6911, 4.8593, 4.7811, 4.7778, 4.7587, 4.8326, 4.6516, 4.8062, 4.6369, 4.8504, 4.5777, 4.7046, 4.7017, 4.6992, 4.6929, 4.6595, 4.5748, 4.7975], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': 'GHz', 'update_time': '2024-08-07 06:35:59'}}, 'relatime': {'T1': {'param_list': [32.0834, 15.3496, 41.7426, 18.8109, 39.4095, 34.1569, 27.2226, 41.4021, 45.1429, 27.3363, 34.0723, 20.9809, 27.1586, 23.2489, 29.316, 28.044, 32.6449, 42.8331, 34.51, 33.292, 11.1108, 19.7031, 36.9693, 28.1199, 25.7801, 33.3957, 42.7922, 44.52, 46.2545, 47.3339, 39.2116, 34.2867, 45.8843, 11.2483, 36.9933, 20.2838, 26.7274, 53.2455, 42.761, 46.6116, 9.2206, 32.0589, 32.6543, 15.9043, 48.8296, 34.7081, 22.8442, 28.4607, 39.6676, 46.495, 42.8035, 38.1672, 31.5051, 39.0114, 29.9296, 41.2292, 39.4186, 35.1132, 32.0341, 37.5697, 41.0691, 41.8796, 51.9372], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': 'us', 'update_time': '2024-08-07 06:42:30'}, 'T2': {'param_list': [3.2126, 2.6164, 2.8801, 2.0339, 2.8415, 2.1791, 2.9795, 2.1216, 4.5112, 3.7053, 2.533, 3.7859, 2.087, 3.3545, 3.8793, 0.8117, 1.6724, 2.3948, 2.6482, 3.0503, 3.1277, 5.2144, 1.4348, 3.8899, 2.3304, 2.3066, 2.6675, 3.569, 3.4687, 4.1659, 2.6226, 3.1679, 2.3717, 3.3037, 4.4307, 3.4005, 2.525, 3.0077, 2.791, 4.9467, 2.6968, 3.6065, 2.0135, 4.4647, 2.1206, 1.9042, 3.9622, 2.2406, 3.7145, 2.6143, 2.7025, 2.7191, 4.4254, 3.6573, 4.3126, 2.1808, 4.7646, 3.0689, 3.0618, 2.4693, 3.017, 1.9217, 5.6671], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': 'us', 'update_time': '2024-08-02 15:28:42'}}, 'singleQubit': {'gate error': {'param_list': [0.18, 0.23, 0.11, 0.19, 0.14, 0.21, 0.16, 0.17, 0.1, 0.11, 0.13, 0.22, 0.14, 0.12, 0.25, 0.17, 0.43, 0.22, 0.22, 0.11, 0.15, 0.16, 0.17, 0.36, 0.12, 2.07, 0.11, 0.12, 0.15, 0.15, 0.15, 0.12, 0.12, 0.55, 0.55, 0.43, 0.14, 0.29, 0.15, 0.15, 0.22, 0.36, 0.15, 0.18, 0.1, 0.15, 0.13, 0.13, 0.1, 0.15, 0.11, 0.35, 0.1, 0.16, 0.11, 0.25, 0.1, 0.45, 0.22, 25.8, 0.14, 0.26, 0.1], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': '%', 'update_time': '2024-08-07 06:52:39'}, 'X/2 amplitude': {'param_list': [0.2843, 0.1516, 0.1162, 0.3667, 0.1932, 0.3206, 0.2995, 0.2799, 0.1236, 0.2737, 0.1642, 0.2303, 0.2602, 0.2674, 0.2599, 0.1679, 0.259, 0.3028, 0.2076, 0.3307, 0.2425, 0.3832, 0.2509, 0.2333, 0.2652, 0.3073, 0.2495, 0.3561, 0.2052, 0.2842, 0.4796, 0.2051, 0.2385, 0.2281, 0.3228, 0.1389, 0.2066, 0.1653, 0.3508, 0.1948, 0.3053, 0.1078, 0.305, 0.208, 0.2535, 0.316, 0.2561, 0.1966, 0.3037, 0.2433, 0.1549, 0.307, 0.1953, 0.1359, 0.1125, 0.2494, 0.2251, 0.1185, 0.2481, 0.2985, 0.2485, 0.172, 0.1773], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': '', 'update_time': '2024-08-07 06:41:16'}, 'X/2 length': {'param_list': [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': '', 'update_time': '2024-08-02 15:28:38'}}} readout {'readoutArray': {'Readout Error': {'param_list': [5.86, 5.58, 7.91, 7.25, 6.31, 4.5, 7.03, 8.04, 4.02, 8.33, 10.52, 5.59, 7.5, 6.3, 4.0, 8.05, 5.34, 4.76, 4.05, 2.02, 5.74, 4.06, 9.38, 4.63, 4.89, 8.3, 3.44, 1.92, 4.48, 3.48, 5.49, 4.96, 4.01, 4.97, 3.57, 3.74, 3.42, 9.45, 5.31, 4.97, 3.12, 7.73, 4.93, 2.38, 6.05, 9.9, 2.7, 3.37, 4.32, 3.73, 3.59, 4.09, 3.63, 4.23, 2.87, 10.34, 16.86, 5.47, 4.39, 21.05, 3.36, 6.97, 6.96], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': '%', 'update_time': '2024-08-07 10:20:12'}, '|0> readout fidelity': {'param_list': [0.9937, 0.9913, 0.9651, 0.9731, 0.9859, 0.9916, 0.9783, 0.9735, 0.986, 0.957, 0.9421, 0.9862, 0.9481, 0.9781, 0.9879, 0.988, 0.9714, 0.9939, 0.9902, 0.985, 0.9911, 0.9898, 0.9798, 0.9913, 0.9841, 0.9881, 0.9893, 0.9927, 0.978, 0.9887, 0.987, 0.9787, 0.9842, 0.9813, 0.9934, 0.9928, 0.9883, 0.9756, 0.9637, 0.9892, 0.9925, 0.9704, 0.9713, 0.9883, 0.9548, 0.8856, 0.9922, 0.9885, 0.9881, 0.9829, 0.9823, 0.9669, 0.9765, 0.9793, 0.9919, 0.9031, 0.8332, 0.9697, 0.9674, 0.942, 0.9838, 0.9382, 0.9172], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': '', 'update_time': '2024-08-07 10:20:12'}, '|1> readout fidelity': {'param_list': [0.8891, 0.897, 0.8768, 0.8819, 0.888, 0.9184, 0.8812, 0.8656, 0.9336, 0.8764, 0.8474, 0.902, 0.9018, 0.896, 0.932, 0.8511, 0.9218, 0.9108, 0.9288, 0.9745, 0.8941, 0.929, 0.8325, 0.916, 0.9181, 0.846, 0.9419, 0.9689, 0.9325, 0.9417, 0.9033, 0.9221, 0.9357, 0.9193, 0.9351, 0.9323, 0.9433, 0.8354, 0.9301, 0.9114, 0.9452, 0.875, 0.9301, 0.9642, 0.9241, 0.9164, 0.9539, 0.944, 0.9255, 0.9425, 0.946, 0.9514, 0.951, 0.936, 0.9508, 0.8902, 0.8296, 0.9209, 0.9447, 0.637, 0.9489, 0.9224, 0.9436], 'qubit_used': ['Q0', 'Q1', 'Q2', 'Q3', 'Q4', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16', 'Q17', 'Q18', 'Q19', 'Q20', 'Q21', 'Q22', 'Q23', 'Q24', 'Q25', 'Q26', 'Q27', 'Q28', 'Q29', 'Q30', 'Q31', 'Q32', 'Q33', 'Q34', 'Q36', 'Q37', 'Q38', 'Q39', 'Q40', 'Q41', 'Q42', 'Q43', 'Q44', 'Q45', 'Q46', 'Q48', 'Q49', 'Q50', 'Q51', 'Q52', 'Q53', 'Q54', 'Q55', 'Q56', 'Q57', 'Q58', 'Q59', 'Q60', 'Q61', 'Q62', 'Q63', 'Q64', 'Q65'], 'unit': '', 'update_time': '2024-08-07 10:20:12'}}} status 0 twoQubitGate {'czGate': {'gate error': {'param_list': [1.24, 1.91, 1.16, 0.91, 1.03, 1.05, 1.35, 1.22, 0.99, 1.54, 1.06, 1.76, 1.6, 1.54, 0.87, 2.34, 1.1, 1.68, 2.26, 2.47, 3.54, 1.54, 1.35, 2.32, 4.78, 0.98, 1.72, 1.89, 2.45, 1.15, 1.2, 1.05, 1.34, 0.94, 1.75, 0.5, 2.06, 1.81, 1.73, 2.35, 1.77, 1.83, 0.85, 1.56, 1.72, 1.57, 1.56, 1.22, 1.9, 3.92, 3.64, 0.61, 2.75, 1.41, 1.44, 3.86, 1.67, 1.71, 4.12, 6.25, 1.11, 4.92, 1.76, 1.25, 1.73, 1.46, 1.38, 1.48, 2.03, 2.41, 1.09, 4.4, 2.2, 2.7, 2.69, 1.03, 2.87, 1.01, 3.93, 1.4, 2.94, 1.37, 2.0, 3.1, 1.35, 3.46], 'qubit_used': ['G0', 'G1', 'G2', 'G3', 'G4', 'G6', 'G7', 'G8', 'G9', 'G11', 'G12', 'G13', 'G14', 'G15', 'G16', 'G17', 'G18', 'G22', 'G23', 'G24', 'G26', 'G27', 'G33', 'G34', 'G35', 'G36', 'G37', 'G39', 'G41', 'G43', 'G44', 'G45', 'G46', 'G47', 'G48', 'G49', 'G50', 'G51', 'G52', 'G55', 'G56', 'G57', 'G58', 'G59', 'G60', 'G61', 'G62', 'G63', 'G66', 'G67', 'G68', 'G69', 'G70', 'G71', 'G72', 'G73', 'G74', 'G77', 'G78', 'G79', 'G80', 'G81', 'G82', 'G83', 'G84', 'G85', 'G88', 'G89', 'G90', 'G91', 'G92', 'G93', 'G94', 'G95', 'G96', 'G97', 'G98', 'G99', 'G100', 'G101', 'G102', 'G105', 'G106', 'G107', 'G108', 'G109'], 'unit': '%', 'update_time': '2024-08-07 09:19:49'}, 'coupling strength': {'param_list': [[-14863822326.1, 3949970777.3], [-17075102279.4, 4042139099.9], [-17072472238.5, 4093045002.9], [-22007890122.7, 4523144360.0], [-33348334307.4, 5120631209.8], [-21082758620.7, 4917241379.3], [-12974050263.0, 3729398012.9], [-16024956166.0, 4388836937.5], [-15924897720.6, 4344067796.6], [-17346580946.8, 4025832846.3], [-21167270602.0, 4667212156.6], [-13944827586.2, 5075862069.0], [-23646522501.5, 4429339567.5], [-35874108708.4, 4967270602.0], [-17090531852.7, 5557744009.4], [-45104324956.2, 5019812974.9], [-35756867329.0, 4753477498.5], [-28306078316.8, 5024956166.0], [-17881998831.1, 4283518410.3], [-18283635301.0, 4074985388.7], [-20348334307.4, 4299941554.6], [-31985330216.2, 4980187025.1], [-18084511981.3, 4267212156.6], [-15493161893.6, 3962010520.2], [-28950087668.0, 4594739918.2], [-18880245470.5, 4264582115.7], [-42919871420.2, 5445645821.2], [-40569783752.2, 4768147282.3], [-25770660432.5, 5746580946.8], [-27394739918.2, 5715604909.4], [-22584570426.7, 4360257159.6], [-34543191116.3, 5118877849.2], [-39081122150.8, 4843015780.2], [-21184570426.7, 4187843366.5], [-27838866160.1, 4492168322.6], [-25098305084.7, 4460315604.9], [-12505961426.1, 3921624780.8], [-12505961426.1, 3859555815.3], [-13653419053.2, 3822443015.8], [-27175920514.3, 4630976037.4], [-29656925774.4, 5067212156.6], [-53419053185.3, 5532670952.7], [-22150087668.0, 4277498538.9], [-26064699006.4, 4487025131.5], [-18634482758.6, 5620689655.2], [-31390473407.4, 5954354178.8], [-18500818235.0, 5333664523.7], [-27881881940.4, 5745704266.5], [-23134541204.0, 4396493278.8], [-16543191116.3, 4187843366.5], [-20283752191.7, 4330040911.7], [-20800993571.0, 4281765049.7], [-21617299824.7, 4396493278.8], [-23196610169.5, 4637872589.1], [-18538807714.8, 4578433664.5], [-15650789012.3, 4031969608.4], [-15341320865.0, 4051782583.3], [-14332787843.4, 5074108708.4], [-20213793103.4, 4234482758.6], [-30351841028.6, 4689538281.7], [-19492285213.3, 4376680303.9], [-27981063705.4, 4549970777.3], [-28499181765.0, 4459438924.6], [-50315488018.7, 5105201636.5], [-20534424313.3, 5962127410.9], [-59055172413.8, 5165517241.4], [-23196610169.5, 4396493278.8], [-21767387492.7, 4232612507.3], [-21181063705.4, 4543074225.6], [-34907831677.4, 5168030391.6], [-20133664523.7, 4204266510.8], [-14656925774.4, 3853419053.2], [-10639625949.7, 3677615429.6], [-15377556984.2, 3967270602.0], [-14767270602.0, 3770660432.5], [-11701694915.3, 3677615429.6], [-13427586206.9, 3903448275.9], [-19010403272.9, 4168907071.9], [-18360374050.3, 4315488018.7], [-17588836937.5, 4218059614.3], [-22762127410.9, 4617182934.0], [-59081881940.4, 5359497370.0], [-17537054354.2, 5428462887.2], [-44520631209.8, 5024196376.4], [-33274108708.4, 4815546464.1], [-20659438924.6, 5526767971.9]], 'qubit_used': ['G0', 'G1', 'G2', 'G3', 'G4', 'G6', 'G7', 'G8', 'G9', 'G11', 'G12', 'G13', 'G14', 'G15', 'G16', 'G17', 'G18', 'G22', 'G23', 'G24', 'G26', 'G27', 'G33', 'G34', 'G35', 'G36', 'G37', 'G39', 'G41', 'G43', 'G44', 'G45', 'G46', 'G47', 'G48', 'G49', 'G50', 'G51', 'G52', 'G55', 'G56', 'G57', 'G58', 'G59', 'G60', 'G61', 'G62', 'G63', 'G66', 'G67', 'G68', 'G69', 'G70', 'G71', 'G72', 'G73', 'G74', 'G77', 'G78', 'G79', 'G80', 'G81', 'G82', 'G83', 'G84', 'G85', 'G88', 'G89', 'G90', 'G91', 'G92', 'G93', 'G94', 'G95', 'G96', 'G97', 'G98', 'G99', 'G100', 'G101', 'G102', 'G105', 'G106', 'G107', 'G108', 'G109'], 'unit': 'Hz', 'update_time': '2024-08-02 15:28:38'}}}
5. Other Functions¶
5.1 QCIS and QASM Instruction Mutual Conversion¶
Below introduces the functionality for instruction set mutual conversion in the cqlib.util
module.
QCIS instructions and QASM instructions are both instruction sets used for quantum circuit programming. For detailed information, please click the links below.
Cqlib defines functions that can achieve mutual conversion of instruction set commands.
# Convert from QCIS circuit to QASM
from cqlib.utils import QcisToQasm
qasm_str = QcisToQasm.convert_qcis_to_qasm(circuit.qcis)
print(f'Converted QASM instruction: {qasm_str}')
# Convert QASM to QCIS
from cqlib.utils import QasmToQcis
qcis_str = QasmToQcis().convert_to_qcis(qasm_str)
print(f'Converted QCIS instruction: {qcis_str}')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
5.2 QCIS Instruction Simplification¶
Cqlib also supports simplifying QCIS instructions. The simplified instructions will be returned in the form of QCIS instructions. In the example below, 3 QCIS instructions will be simplified to 2 QCIS instructions.
from cqlib.utils import QCIS_Simplify
qcis = """
Y2M Q1
X2P Q1
X2P Q1
"""
new_qcis = QCIS_Simplify().simplify(qcis)
origin_line = qcis.count('\n') - 1
new_line = new_qcis.count('\n') - 1
print(f"Circuit lines before simplification: {origin_line}")
print(f"Circuit lines after simplification: {new_line}")
print(f"\nCircuit after simplification: {new_qcis}")
1 2 3 4 5 6 |
|
5.3 Virtual Circuit Mapping¶
The SDK provides a virtual circuit mapping algorithm based on Monte Carlo Tree Search (MCTS), which can output circuits mapped to the real physical machine topology structure based on the topology graph of the selected quantum computer and the input virtual quantum circuit.
from cqlib.mapping import transpile_qcis
from cqlib.circuits import Circuit, Parameter
c2 = Circuit(qubits=[0, 1])
c2.rz(0, 0.0)
c2.x2p(1)
c2.y2p(0)
c2.rz(1, 0.9272952180016122)
c2.rz(0, 3.1415910000000005)
c2.x2m(1)
c2.rz(1, 0.0)
c2.y2m(1)
c2.cz(0, 1)
c2.y2p(1)
c2.rz(1, 0.0)
c2.x2p(1)
c2.rz(1, -0.9272952180016122)
c2.x2m(1)
c2.rz(1, 0.0)
c2.y2m(1)
c2.cz(0, 1)
c2.y2p(1)
c2.y2m(0)
c2.rz(1, 0.0)
c2.rz(0, -3.1415910000000005)
c2.x2p(1)
c2.rz(1, 0.9272952180016122)
c2.x2m(1)
c2.rz(1, 0.0)
c2.measure(0)
c2.measure(1)
qcis_virtual_circuit = c2.qcis
# Check if the qcis circuit is valid after mapping
before_mapping_check_res = platform.qcis_check_regular(qcis_virtual_circuit)
print(f'Before mapping, validity check result: {before_mapping_check_res}')
# Establish mapping between virtual circuit and real circuit
circuit, initial_layout, swap_mapping, mapping_virtual_to_final = transpile_qcis(qcis_virtual_circuit, platform)
qcis_after_mapping = circuit.qcis.upper()
print(f'QCIS result after mapping: {qcis_after_mapping}')
# Check if the qcis circuit is valid after mapping
after_mapping_check_res = platform.qcis_check_regular(qcis_after_mapping)
print(f'After mapping, validity check result: {after_mapping_check_res}')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|