3
3
of circuits of mixed phase gadgets.
4
4
"""
5
5
6
- from typing import (cast , Collection , Dict , FrozenSet , Iterator , List ,
6
+ from typing import (cast , Collection , Dict , FrozenSet , Iterator , List , Any ,
7
7
Optional , overload , Sequence , Tuple , Union , Literal )
8
8
import numpy as np # type: ignore
9
9
from numpy .typing import NDArray
@@ -79,8 +79,8 @@ def choose_column(options, row, m):
79
79
# TODO possibly pick the row here now that the full steiner_tree has a 1
80
80
for parent , child in traversal :
81
81
add_cnot (child , parent , m , cnots )
82
- assert ( sum (m [:,col ]) == 1 )
83
- assert ( m [row , col ] == 1 )
82
+ assert sum (m [:,col ]) == 1
83
+ assert m [row , col ] == 1
84
84
85
85
# Reduce the row
86
86
ones_in_the_row = [i for i in columns_to_eliminate if m [row , i ]== 1 ]
@@ -100,11 +100,11 @@ def choose_column(options, row, m):
100
100
add_cnot (parent , child , m , cnots )
101
101
for parent , child in reversed (traversal ):
102
102
add_cnot (parent , child , m , cnots )
103
- assert ( m [row , col ] == 1 )
104
- assert ( sum (m [row ,:]) == 1 )
105
- assert ( sum (m [:,col ]) == 1 )
106
- assert ( m [row , col ] == 1 )
107
- assert ( sum (m [row ,:]) == 1 )
103
+ assert m [row , col ] == 1
104
+ assert sum (m [row ,:]) == 1
105
+ assert sum (m [:,col ]) == 1
106
+ assert m [row , col ] == 1
107
+ assert sum (m [row ,:]) == 1
108
108
qubits_to_process .remove (row )
109
109
columns_to_eliminate .remove (col )
110
110
new_mapping [row ] = col
@@ -292,6 +292,18 @@ def clone(self) -> "CXCircuitLayer":
292
292
Returns a copy of this CX layer.
293
293
"""
294
294
return CXCircuitLayer (self .topology , self .gates )
295
+
296
+
297
+ def to_qiskit (self ) -> Any :
298
+ try :
299
+ # pylint: disable = import-outside-toplevel
300
+ from qiskit .circuit import QuantumCircuit
301
+ except ModuleNotFoundError as e :
302
+ raise ModuleNotFoundError ("You must install the 'qiskit' library." ) from e
303
+ circuit = QuantumCircuit (self .topology .num_qubits )
304
+ for ctrl , trgt in self .gates :
305
+ circuit .cx (ctrl , trgt )
306
+ return circuit
295
307
296
308
def draw (self , layout : str = "kamada_kawai" , * ,
297
309
figsize : Optional [Tuple [int , int ]] = None ,
@@ -472,6 +484,21 @@ def clone(self) -> "CXCircuit":
472
484
"""
473
485
return CXCircuit (self .topology , [l .clone () for l in self ])
474
486
487
+ def to_qiskit (self , method :Literal ["permrowcol" , "naive" ]= "naive" , reallocate :bool = False ) -> Any :
488
+ try :
489
+ # pylint: disable = import-outside-toplevel
490
+ from qiskit .circuit import QuantumCircuit
491
+ except ModuleNotFoundError as e :
492
+ raise ModuleNotFoundError ("You must install the 'qiskit' library." ) from e
493
+ circuit = QuantumCircuit (self .topology .num_qubits )
494
+ if method == "naive" :
495
+ cxs = self
496
+ else :
497
+ cxs = CXCircuit .from_parity_matrix (self .parity_matrix (), self .topology , reallocate = reallocate , method = method )
498
+ for layer in cxs ._layers :
499
+ circuit .compose (layer .to_qiskit (), inplace = True )
500
+ return circuit
501
+
475
502
def draw (self , layout : str = "kamada_kawai" , * ,
476
503
figsize : Optional [Tuple [int , int ]] = None ,
477
504
zcolor : str = "#CCFFCC" ,
@@ -512,7 +539,7 @@ def from_parity_matrix(matrix:NDArray, topology:Topology, parities_as_columns:bo
512
539
topology (Topology): The target device topology
513
540
parities_as_columns (bool, optional): Whether the parities in the matrix are column-wise or row-wise. Defaults to False, i.e. row-wise.
514
541
reallocate (bool, optional): Whether the qubits can be reallocated to different registers, i.e. synthesis up to permutation. Defaults to False.
515
- method (Literal[" permrowcol" ], optional): Which synthesis method should be used. Currently only permrowcol is available.
542
+ method (Literal[" permrowcol" ], optional): Which synthesis method should be used. Currently only permrowcol is available.
516
543
517
544
Returns:
518
545
CXCircuit: Synthesized circuit
@@ -522,7 +549,7 @@ def from_parity_matrix(matrix:NDArray, topology:Topology, parities_as_columns:bo
522
549
layers = []
523
550
current_layer = []
524
551
for cnot in cnots :
525
- assert ( cnot [0 ] in topology .adjacent (cnot [1 ]) ) # Double check that the cnot is allowed
552
+ assert cnot [0 ] in topology .adjacent (cnot [1 ]) # Double check that the cnot is allowed
526
553
if any ([c in cnot or t in cnot for c ,t in current_layer ]):
527
554
layer = CXCircuitLayer (topology , current_layer )
528
555
layers .append (layer )
0 commit comments