QuarkCircuit: Construction, Visualization, and Transpilation of Quantum Circuits
Installation
pip install quarkcircuit
Construct and visualize a quantum circuit
In [1]:
Copied!
from quark.circuit import QuantumCircuit
from quark.circuit import QuantumCircuit
In [2]:
Copied!
nqubits = 3
qc = QuantumCircuit(nqubits)
qc.h(0)
for i in range(1,nqubits):
qc.cx(0,i)
qc.barrier()
qc.measure_all()
qc.draw()
nqubits = 3
qc = QuantumCircuit(nqubits)
qc.h(0)
for i in range(1,nqubits):
qc.cx(0,i)
qc.barrier()
qc.measure_all()
qc.draw()
q[0] ────H────●────●────░────M──────────────
│ │ ░ │
q[1] ─────────X────│────░────│────M─────────
│ ░ │ │
q[2] ──────────────X────░────│────│────M────
│ │ │
c: 3/═══════════════════════════════════════
0 1 2
Note: For better circuit display, please set your chrome or VS Code to a monospaced font, such as "Consolas".
Transpile the quantum circuit to match the attributes of a specific quantum device
Step1: Load BAQIS superconducting quantum computer
In [3]:
Copied!
from quark.circuit import Backend
from quark.circuit import Backend
In [4]:
Copied!
chip_name = 'Baihua'
chip_backend = Backend(chip_name)
chip_name = 'Baihua'
chip_backend = Backend(chip_name)
Baihua configuration loading done! The last calibration time was 2025-03-25 16:32:15
In [7]:
Copied!
chip_backend.draw(show_couplers_fidelity=True,show_quibts_attributes='T1',save_svg_fname='baihua_chip_fig',show_qubits_index=True,show_couplers_index=True)
chip_backend.draw(show_couplers_fidelity=True,show_quibts_attributes='T1',save_svg_fname='baihua_chip_fig',show_qubits_index=True,show_couplers_index=True)
Step2: Transpile
In [8]:
Copied!
from quark.circuit import Transpiler
from quark.circuit import Transpiler
In [14]:
Copied!
# chip backend
qct = Transpiler(chip_backend).run(qc,target_qubits=[],optimize_level=1)
print(qct.depth)
qct.draw_simply()
# chip backend
qct = Transpiler(chip_backend).run(qc,target_qubits=[],optimize_level=1)
print(qct.depth)
qct.draw_simply()
Physical qubits layout [21, 22, 23] are derived from the chip backend priority qubits, with the corresponding coupling being [(21, 22), (22, 23)].
The average fidelity of the coupler(s) between the selected qubits is 0.987, and the variance of the fidelity is 1.0000000000000019e-06.
initial mapping -----> best mapping -----> final mapping
P : V -----> P : V -----> P : V
21 : 0 -----> 21 : 1 -----> 21 : 1
22 : 1 -----> 22 : 0 -----> 22 : 0
23 : 2 -----> 23 : 2 -----> 23 : 2
Mapping to basic gates done !
5
q[21] ────U(0.5π,0.0,1.0π)─────Z────U(0.5π,0.0,1.0π)──────────────────────────░─────────M─────────
│ ░ │
q[22] ────U(0.5π,0.0,1.0π)─────●────────────●─────────────────────────────────░────M────│─────────
│ ░ │ │
q[23] ────U(0.5π,0.0,1.0π)──────────────────Z────────────U(0.5π,0.0,1.0π)─────░────│────│────M────
│ │ │
c: 3/════════════════════════════════════════════════════════════════════════════════════════════
0 1 2
Step3: Submit the circuit to "Baihua" backend for execution via QuarkStudio
In [ ]:
Copied!
from quark import Task
# Log in SQCLab https://quafu-sqc.baqis.ac.cn/login to obtain a token.
yourtoken = '5nCmgtOdsCk8.jZWl8n`8T`KCvNsGObH:dMjnGg``wN/Rg3R{O2R{O15DP3lUO6NEP{dUN7JDd5WnJtJDPxpkO1pUOyBjNx1TNx1TOzBkNjpkJ1GXbjxjJvOnMkGnM{mXdiKHR5i4cimHfjpkJzW3d2Kzf'
tmgr = Task(yourtoken)
from quark import Task
# Log in SQCLab https://quafu-sqc.baqis.ac.cn/login to obtain a token.
yourtoken = '5nCmgtOdsCk8.jZWl8n`8T`KCvNsGObH:dMjnGg``wN/Rg3R{O2R{O15DP3lUO6NEP{dUN7JDd5WnJtJDPxpkO1pUOyBjNx1TNx1TOzBkNjpkJ1GXbjxjJvOnMkGnM{mXdiKHR5i4cimHfjpkJzW3d2Kzf'
tmgr = Task(yourtoken)
In [ ]:
Copied!
task = {
'chip': 'Baihua', # chip name
'name': 'YourFirstQuantumTask', # task name
'circuit':qct.to_openqasm2, # the transpiled circuit written in openqasm 2.0
'compile': False, # No need to execute transpile again. Pre-transpiling is recommended.
}
# or
#task = {
# 'chip': 'Baihua', # chip name
# 'name': 'YourFirstQuantumTask', # task name
# 'circuit':qc.to_openqasm2, # the original circuit written in openqasm 2.0
# 'compile': True, # The server will default to utilizing quarkcircuit for circuit validation and compilation.
# 'options':{'target_qubits':[46,47,48]}
#}
task = {
'chip': 'Baihua', # chip name
'name': 'YourFirstQuantumTask', # task name
'circuit':qct.to_openqasm2, # the transpiled circuit written in openqasm 2.0
'compile': False, # No need to execute transpile again. Pre-transpiling is recommended.
}
# or
#task = {
# 'chip': 'Baihua', # chip name
# 'name': 'YourFirstQuantumTask', # task name
# 'circuit':qc.to_openqasm2, # the original circuit written in openqasm 2.0
# 'compile': True, # The server will default to utilizing quarkcircuit for circuit validation and compilation.
# 'options':{'target_qubits':[46,47,48]}
#}
In [184]:
Copied!
tid = tmgr.run(task, repeat=10) # shots = repeat*1024
print(tid) # tid refers to task id
tid = tmgr.run(task, repeat=10) # shots = repeat*1024
print(tid) # tid refers to task id
2503251623276948015
In [ ]:
Copied!
res = tmgr.result(tid)
res
res = tmgr.result(tid)
res
Out[ ]:
{'count': {'000': 4305,
'011': 649,
'111': 4210,
'100': 482,
'001': 111,
'101': 316,
'110': 83,
'010': 84},
'corrected': {},
'chip': 'Baihua',
'circuit': 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[3];\ncreg c[3];\nh q[0];\ncx q[0],q[1];\ncx q[0],q[2];\nbarrier q[0],q[1],q[2];\nmeasure q[0] -> c[0];\nmeasure q[1] -> c[1];\nmeasure q[2] -> c[2];',
'transpiled': 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[49];\ncreg c[3];\nh q[47];\ncx q[47],q[46];\ncx q[47],q[48];\nbarrier q[47],q[46],q[48];\nmeasure q[47] -> c[0];\nmeasure q[46] -> c[1];\nmeasure q[48] -> c[2];',
'qlisp': "[(('U', 1.5707963267948966, 0.0, 3.141592653589793), 'Q46'),\n(('U', 1.5707963267948966, 0.0, 3.141592653589793), 'Q47'),\n(('U', 1.5707963267948966, 0.0, 3.141592653589793), 'Q48'),\n('CZ', ('Q47', 'Q46')),\n(('U', 1.5707963267948966, 0.0, 3.141592653589793), 'Q46'),\n('CZ', ('Q47', 'Q48')),\n(('U', 1.5707963267948966, 0.0, 3.141592653589793), 'Q48'),\n('Barrier', ('Q47', 'Q46', 'Q48')),\n(('Measure', 1), 'Q46'),\n(('Measure', 0), 'Q47'),\n(('Measure', 2), 'Q48')]",
'tid': 2503251623276948015,
'error': '',
'status': 'Finished',
'created': '2025-03-25-16-29-55',
'finished': '2025-03-25-16-30-00'}
In [205]:
Copied!
import matplotlib.pyplot as plt
data = res['count']
bases = sorted(data)
count = [data[base] for base in bases]
plt.bar(bases, count)
plt.xticks(rotation=45)
import matplotlib.pyplot as plt
data = res['count']
bases = sorted(data)
count = [data[base] for base in bases]
plt.bar(bases, count)
plt.xticks(rotation=45)
Out[205]:
([0, 1, 2, 3, 4, 5, 6, 7], [Text(0, 0, '000'), Text(1, 0, '001'), Text(2, 0, '010'), Text(3, 0, '011'), Text(4, 0, '100'), Text(5, 0, '101'), Text(6, 0, '110'), Text(7, 0, '111')])