Skip to content

Commit 367a6fa

Browse files
Aeryliay-richie-y
andauthored
Phasepolynomial synth (#20)
Co-authored-by: Richie Yeung <[email protected]>
1 parent 968593f commit 367a6fa

File tree

4 files changed

+372
-38
lines changed

4 files changed

+372
-38
lines changed

pauliopt/phase/cx_circuits.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
of circuits of mixed phase gadgets.
44
"""
55

6-
from typing import (cast, Collection, Dict, FrozenSet, Iterator, List,
6+
from typing import (cast, Collection, Dict, FrozenSet, Iterator, List, Any,
77
Optional, overload, Sequence, Tuple, Union, Literal)
88
import numpy as np # type: ignore
99
from numpy.typing import NDArray
@@ -79,8 +79,8 @@ def choose_column(options, row, m):
7979
# TODO possibly pick the row here now that the full steiner_tree has a 1
8080
for parent, child in traversal:
8181
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
8484

8585
# Reduce the row
8686
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):
100100
add_cnot(parent, child, m, cnots)
101101
for parent, child in reversed(traversal):
102102
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
108108
qubits_to_process.remove(row)
109109
columns_to_eliminate.remove(col)
110110
new_mapping[row] = col
@@ -292,6 +292,18 @@ def clone(self) -> "CXCircuitLayer":
292292
Returns a copy of this CX layer.
293293
"""
294294
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
295307

296308
def draw(self, layout: str = "kamada_kawai", *,
297309
figsize: Optional[Tuple[int, int]] = None,
@@ -472,6 +484,21 @@ def clone(self) -> "CXCircuit":
472484
"""
473485
return CXCircuit(self.topology, [l.clone() for l in self])
474486

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+
475502
def draw(self, layout: str = "kamada_kawai", *,
476503
figsize: Optional[Tuple[int, int]] = None,
477504
zcolor: str = "#CCFFCC",
@@ -512,7 +539,7 @@ def from_parity_matrix(matrix:NDArray, topology:Topology, parities_as_columns:bo
512539
topology (Topology): The target device topology
513540
parities_as_columns (bool, optional): Whether the parities in the matrix are column-wise or row-wise. Defaults to False, i.e. row-wise.
514541
reallocate (bool, optional): Whether the qubits can be reallocated to different registers, i.e. synthesis up to permutation. Defaults to False.
515-
method (Literal[&quot;permrowcol&quot;], 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.
516543
517544
Returns:
518545
CXCircuit: Synthesized circuit
@@ -522,7 +549,7 @@ def from_parity_matrix(matrix:NDArray, topology:Topology, parities_as_columns:bo
522549
layers = []
523550
current_layer = []
524551
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
526553
if any([c in cnot or t in cnot for c,t in current_layer]):
527554
layer = CXCircuitLayer(topology, current_layer)
528555
layers.append(layer)

0 commit comments

Comments
 (0)