# 比特映射

## Qubit Node

QubitNode表示量子芯片中的比特，当前，主要包含四个属性：

• qubit_id：量子比特的唯一识别码

• color：在绘制拓扑结构图时，量子比特的颜色， 默认为 #000000

• poi_x：在绘制拓扑结构图时，量子比特的 x 位置，默认为 0.0

• poi_y：在绘制拓扑结构图时，量子比特的 y 位置，默认为 0.0

[1]:

from mindquantum.device import QubitNode

q0 = QubitNode(0, '#121212', 0, 0)

print(f"qubit id: {q0.qubit_id}, with color: {q0.color}, and position ({q0.poi_x}, {q0.poi_y})")

qubit id: 0, with color: #121212, and position (0, 0)


lhs

op

rhs

q0

>>

q1

q1

q0

<<

q1

q0

q0

>

q1

q1

q0

<

q1

q0

[2]:

q0, q1, q2 = QubitNode(0), QubitNode(1), QubitNode(2)

q0 >> q1 >> q2

[2]:

<mindquantum.device.topology.QubitNode at 0x10f342710>


### QubitsTopology

QubitsTopology 类是量子比特的容器，可以有效地组织和操控量子比特，例如当我们想要构建一个一维链的量子比特时，我们可以如下操作：

[3]:

from mindquantum.device import QubitsTopology, QubitNode

n = 5

topology = QubitsTopology([QubitNode(i, poi_x=i) for i in range(n)])
print(topology)

<mindquantum.device.topology.QubitsTopology object at 0x13d07f850>


MindQuantum 中自带了绘制量子比特拓扑结构图的接口，接口如下：

[4]:

from mindquantum.io.display import draw_topology

draw_topology(topology)

[4]:


[5]:

left_node = topology[0]
for i in range(1, n):
left_node = left_node << topology[i]

draw_topology(topology)

[5]:


[6]:

from mindquantum.device import LinearQubits, GridQubits
from IPython.display import display_svg

t1 = LinearQubits(3)
t2 = GridQubits(4, 5)

display_svg(draw_topology(t1))
display_svg(draw_topology(t2))


[7]:

t2.remove_qubit_node(6)

draw_topology(t2)

[7]:


[8]:

t2.isolate_with_near(13)

draw_topology(t2)

[8]:


[9]:

t2.set_color(7, '#ff0000')

draw_topology(t2)

[9]:


[10]:

t2.set_position(4, 5.0, 0.5)

draw_topology(t2)

[10]:


[11]:

t2[4] << t2[14]

draw_topology(t2)

[11]:


[12]:

t2.edges_with_id()

[12]:

{(0, 1),
(0, 5),
(1, 2),
(2, 3),
(2, 7),
(3, 4),
(3, 8),
(4, 9),
(4, 14),
(5, 10),
(7, 8),
(7, 12),
(8, 9),
(9, 14),
(10, 11),
(10, 15),
(11, 12),
(11, 16),
(12, 17),
(14, 19),
(15, 16),
(16, 17),
(17, 18),
(18, 19)}


### Qubit Mapping

[13]:

from mindquantum.core.circuit import Circuit

circ = Circuit().rx('a', 0).rx('b', 1).rx('c', 2).x(1, 0).x(2, 1).x(0, 2)
circ.svg()

[13]:


[14]:

t = GridQubits(2, 2)

draw_topology(t)

[14]:


[15]:

from mindquantum.algorithm.mapping import SABRE

solver = SABRE(circ, t)
new_circ, init_mapping, final_mapping = solver.solve(5, 0.5, 0.3, 0.2)


[16]:

new_circ.svg()

[16]:


[17]:

circ.svg()

[17]:


[18]:

print(f"initial mapping: {init_mapping}")
print(f"  final mapping: {final_mapping}")

initial mapping: [1, 0, 2, 3]
final mapping: [3, 0, 2, 1]


[19]:

draw_topology(t, new_circ)

[19]:


[20]:

from mindquantum.utils.show_info import InfoTable
InfoTable('mindquantum', 'scipy', 'numpy')

[20]:

Software Version
mindquantum0.9.0
scipy1.7.3
numpy1.21.6
System Info
Python3.7.10
OSDarwin x86_64
Memory8.59 GB