mindquantum.device.QubitsTopology

View Source On Gitee
class mindquantum.device.QubitsTopology(qubits: List[QubitNode])[source]

Topology of qubit in physical device.

Topology is construct by different QubitNode, and you can set the property of each qubit node directly.

Parameters

qubits (List[QubitNode]) – All qubit nodes in this topology.

Examples

>>> from mindquantum.device import QubitsTopology
>>> from mindquantum.device import QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology[0] >> topology[1]
>>> topology.is_coupled_with(0, 1)
True
>>> topology.set_color(0, "#121212")
>>> topology[0].color
'#121212'
add_qubit_node(qubit: QubitNode)[source]

Add a qubit node into this topology.

Parameters

qubit (QubitNode) – the qubit you want to add into this topology.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(2)])
>>> topology.add_qubit_node(QubitNode(2));
>>> topology.all_qubit_id()
{0, 1, 2}
all_qubit_id()[source]

Get all qubit id.

Returns

Set[int], all qubit id in this qubit topology.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(2)])
>>> topology.add_qubit_node(QubitNode(2));
>>> topology.all_qubit_id()
{0, 1, 2}
choose(ids: List[int])[source]

Choose qubit nodes based on given qubit id.

Parameters

ids (List[int]) – A list of qubit node id.

Returns

List[QubitNode], a list of qubit node based on given qubit node id.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology[0] >> topology[1] >> topology[2]
>>> nodes = topology.choose([0, 1])
>>> print(nodes[0].qubit_id, nodes[1].qubit_id)
0 1
compress()[source]

Relabeling the qubit id so that the qubit id in new topology will start from 0.

Returns

Tuple[QubitsTopology, Dict[int, int]], the first element of return is the new compressed topology, and the second element of return is the qubit id map with key be the qubit id in old topology and value be the qubit id in new topology.

Examples

>>> from mindquantum.device import LinearQubits
>>> topo1 = LinearQubits(5)
>>> topo1.remove_qubit_node(0)
>>> topo1.remove_qubit_node(2)
>>> topo2, qubit_map = topo1.compress()
>>> print(topo2.edges_with_id())
{(1, 2)}
>>> print(qubit_map)
{1: 0, 3: 1, 4: 2}
edges_with_id()[source]

Get edges with id of two connected qubits.

Returns

Set[Tuple[int, int]], all connected edges in this qubit topology.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(2)])
>>> topology[0] << topology[1]
>>> topology.edges_with_id()
{(0, 1)}
edges_with_poi()[source]

Get edges with position of two connected qubits.

Returns

Set[Tuple[Tuple[float, float], Tuple[float, float]]], the x and y position of two connected qubits.

Examples

>>> from mindquantum.device import QubitNode, QubitsTopology
>>> q0 = QubitNode(0, poi_x=0, poi_y=0)
>>> q1 = QubitNode(1, poi_x=1, poi_y=0)
>>> q0 >> q1
>>> topology = QubitsTopology([q0, q1])
>>> topology.edges_with_poi()
{((0, 0), (1, 0))}
get_edge_color(qubit_id1: int, qubit_id2: int)[source]

Get color of edge.

The order of qubit_id1 and qubit_id2 does not matter.

Parameters
  • qubit_id1 (int) – The first qubit of edge.

  • qubit_id2 (int) – The second qubit of edge.

has_qubit_node(qubit_id: int)[source]

Check whether a qubit is in this topology.

Parameters

qubit_id (int) – the id of qubit you want to check.

Returns

bool, whether this topology has qubit node with given id.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(2)])
>>> topology.has_qubit_node(0)
True
is_coupled_with(id1: int, id2: int)[source]

Check whether two qubit nodes are coupled.

Parameters
  • id1 (int) – the id of one qubit you want to check.

  • id2 (int) – the id of the other qubit you want to check.

Returns

bool, whether two qubits node with given ids coupled.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(2)])
>>> topology.is_coupled_with(0, 1)
False
>>> topology[0] >> topology[1]
>>> topology.is_coupled_with(0, 1)
True
isolate_with_near(qubit_id: int)[source]

Disconnect with all coupling qubits.

Parameters

qubit_id (int) – the id of qubit you want to disconnect with all nearby qubits.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology[0] >> topology[1] >> topology[2]
>>> topology.edges_with_id()
{(0, 1), (1, 2)}
>>> topology.isolate_with_near(1)
>>> topology.edges_with_id()
set()
n_edges()[source]

Get total connected edge number.

Returns

int, the edge number of this qubit topology.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology[0] >> topology[1] >> topology[2]
>>> topology.n_edges()
2
remove_isolate_node()[source]

Remove qubit node that do not connect with any other qubits.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology[0] >> topology[1] >> topology[2]
>>> topology.edges_with_id()
{(0, 1), (1, 2)}
>>> topology.isolate_with_near(1)
>>> topology.all_qubit_id()
{0, 1, 2}
>>> topology.remove_isolate_node()
>>> topology.all_qubit_id()
set()
remove_qubit_node(qubit_id: int)[source]

Remove a qubit node out of this topology.

Parameters

qubit_id (int) – the id of qubit you want to remove.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology.remove_qubit_node(1)
>>> topology.all_qubit_id()
{0, 2}
select(ids: List[int])[source]

Select certain qubit nodes to generate a new topology.

Parameters

ids (List[int]) – A list of qubit node id.

Returns

QubitsTopology, a new topology while keeping the connection property.

Examples

>>> from mindquantum.device import LinearQubits
>>> t1 = LinearQubits(4)
>>> t2 = t1.select([0, 1, 2])
>>> t2.edges_with_id()
{(0, 1), (1, 2)}
set_color(qubit_id: int, color: str)[source]

Set color of certain qubit.

Parameters
  • qubit_id (int) – the id of qubit you want to change color.

  • color (str) – the new color.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology.set_color(0, "#ababab")
>>> topology[0].color
'#ababab'
set_edge_color(qubit_id1: int, qubit_id2: int, color: str)[source]

Set color of edge.

The order of qubit_id1 and qubit_id2 does not matter.

Parameters
  • qubit_id1 (int) – The first qubit of edge.

  • qubit_id2 (int) – The second qubit of edge.

  • color (str) – The color of edge.

set_position(qubit_id: int, poi_x: float, poi_y: float)[source]

Set position of a certain qubit.

Parameters
  • qubit_id (int) – the id of qubit you want to change position.

  • poi_x (float) – new x position.

  • poi_y (float) – new y position.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology.set_position(0, 1, 1)
>>> topology[0].poi_x, topology[0].poi_y
(1, 1)
show(method: Optional[AVA_SHOW_METHOD] = None)[source]

Display the topology.

Parameters

method (str) – The method you want to display the topology. If None, we will use default method, which is 'mpl' in terminal environment and 'svg' in jupyter notebook environment. You can also set it to 'mpl' or 'svg' manually. Default: None.

size()[source]

Get total qubit number.

Returns

int, the total qubit number.

Examples

>>> from mindquantum.device import QubitsTopology, QubitNode
>>> topology = QubitsTopology([QubitNode(i) for i in range(3)])
>>> topology.size()
3