Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
[bugfix] update rebases and squash to new pytket API
Browse files Browse the repository at this point in the history
* use auto_squash and auto_rebase
* provide full gateset as one argument to RebaseCustom
* remove use of Rebase<Target> passes

requires changes from
CQCL/tket#234

and
CQCL/tket#214
  • Loading branch information
ss2165 committed Feb 17, 2022
1 parent e840aaa commit 94f67c1
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 183 deletions.
16 changes: 3 additions & 13 deletions modules/pytket-aqt/pytket/extensions/aqt/backends/aqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
DecomposeBoxes,
RenameQubitsPass,
SimplifyInitial,
auto_rebase_pass,
)
from pytket.predicates import ( # type: ignore
GateSetPredicate,
Expand Down Expand Up @@ -376,19 +377,8 @@ def _translate_aqt(circ: Circuit) -> Tuple[List[List], str]:
return (gates, json.dumps(measures))


def _aqt_rebase() -> BasePass:
# CX replacement
c_cx = Circuit(2)
c_cx.Ry(0.5, 0).Rx(0.5, 0)
c_cx.Rx(-0.5, 1)
c_cx.add_gate(OpType.XXPhase, 0.5, [0, 1])
c_cx.Ry(0.5, 0).Rx(-1, 0)
c_cx.add_phase(-0.25)

# TK1 replacement
c_tk1 = lambda a, b, c: Circuit(1).Rx(-0.5, 0).Ry(c, 0).Rx(b, 0).Ry(a, 0).Rx(0.5, 0)

return RebaseCustom({OpType.XXPhase}, c_cx, {OpType.Rx, OpType.Ry}, c_tk1)
def _aqt_rebase() -> RebaseCustom:
return auto_rebase_pass({OpType.XXPhase, OpType.Rx, OpType.Ry})


_xcirc = Circuit(1).Rx(1, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
DecomposeBoxes,
SimplifyInitial,
)
from pytket._tket.circuit._library import _TK1_to_RzRx # type: ignore
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.predicates import ( # type: ignore
ConnectivityPredicate,
Expand Down Expand Up @@ -355,14 +356,13 @@ def __init__(
self._req_preds.append(ConnectivityPredicate(arch))

self._rebase_pass = RebaseCustom(
self._multiqs,
self._multiqs | self._singleqs,
Circuit(),
self._singleqs,
lambda a, b, c: Circuit(1).Rz(c, 0).Rx(b, 0).Rz(a, 0),
_TK1_to_RzRx,
)
self._squash_pass = SquashCustom(
self._singleqs,
lambda a, b, c: Circuit(1).Rz(c, 0).Rx(b, 0).Rz(a, 0),
_TK1_to_RzRx,
)

@staticmethod
Expand Down
22 changes: 8 additions & 14 deletions modules/pytket-cirq/pytket/extensions/cirq/backends/cirq.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from pytket.transform import Transform # type: ignore
from pytket.passes import ( # type: ignore
BasePass,
RebaseCirq,
auto_rebase_pass,
SequencePass,
RebaseCustom,
SquashCustom,
Expand All @@ -42,6 +42,7 @@
RemoveRedundancies,
FullPeepholeOptimise,
)
from pytket._tket.circuit._library import _TK1_to_PhasedXRz, _CX # type: ignore
from pytket.predicates import ( # type: ignore
GateSetPredicate,
NoClassicalControlPredicate,
Expand Down Expand Up @@ -89,7 +90,7 @@ def __init__(self) -> None:
self._gate_set_predicate = _regular_gate_set_predicate

def rebase_pass(self) -> BasePass:
return RebaseCirq()
return auto_rebase_pass({OpType.CZ, OpType.PhasedX, OpType.Rz})

@property
def required_predicates(self) -> List[Predicate]:
Expand Down Expand Up @@ -498,16 +499,8 @@ def package_results(
return all_backres


def _tk1_to_phasedxrz(a: float, b: float, c: float) -> Circuit:
circ = Circuit(1)
circ.Rz(a + c, 0)
circ.add_gate(OpType.PhasedX, [b, a], [0])
RemoveRedundancies().apply(circ)
return circ


_cirq_squash = SquashCustom(
{OpType.PhasedX, OpType.Rz, OpType.Rx, OpType.Ry}, _tk1_to_phasedxrz
{OpType.PhasedX, OpType.Rz, OpType.Rx, OpType.Ry}, _TK1_to_PhasedXRz
)

_regular_gate_set_predicate = GateSetPredicate(
Expand Down Expand Up @@ -559,14 +552,12 @@ def _tk1_to_phasedxrz(a: float, b: float, c: float) -> Circuit:


def _tk1_to_phasedxrz_clifford(a: float, b: float, c: float) -> Circuit:
circ = _tk1_to_phasedxrz(a, b, c)
circ = _TK1_to_PhasedXRz(a, b, c)
Transform.RebaseToCliffordSingles().apply(circ)
return circ


_partial_clifford_rebase = RebaseCustom(
{OpType.CX, OpType.CZ},
Circuit(2).CX(0, 1),
{
OpType.PhasedX,
OpType.Rz,
Expand All @@ -580,6 +571,9 @@ def _tk1_to_phasedxrz_clifford(a: float, b: float, c: float) -> Circuit:
OpType.S,
OpType.Sdg,
OpType.H,
OpType.CX,
OpType.CZ,
},
_CX(),
_tk1_to_phasedxrz_clifford,
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
SequencePass,
SynthesiseTket,
RemoveRedundancies,
RebaseHQS,
SquashHQS,
FullPeepholeOptimise,
DecomposeBoxes,
DecomposeClassicalExp,
SimplifyInitial,
auto_rebase_pass,
auto_squash_pass,
)
from pytket.predicates import ( # type: ignore
GateSetPredicate,
Expand Down Expand Up @@ -254,11 +254,12 @@ def required_predicates(self) -> List[Predicate]:
return preds

def rebase_pass(self) -> BasePass:
return RebaseHQS()
return auto_rebase_pass(_GATE_SET)

def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
assert optimisation_level in range(3)
passlist = [DecomposeClassicalExp(), DecomposeBoxes()]
squash = auto_squash_pass({OpType.PhasedX, OpType.Rz})
if optimisation_level == 0:
return SequencePass(passlist + [self.rebase_pass()])
elif optimisation_level == 1:
Expand All @@ -268,7 +269,7 @@ def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
SynthesiseTket(),
self.rebase_pass(),
RemoveRedundancies(),
SquashHQS(),
squash,
SimplifyInitial(
allow_classical=False, create_all_qubits=True, xcirc=_xcirc
),
Expand All @@ -281,7 +282,7 @@ def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
FullPeepholeOptimise(),
self.rebase_pass(),
RemoveRedundancies(),
SquashHQS(),
squash,
SimplifyInitial(
allow_classical=False, create_all_qubits=True, xcirc=_xcirc
),
Expand Down
4 changes: 2 additions & 2 deletions modules/pytket-honeywell/tests/convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from pytket.circuit import Circuit, OpType # type: ignore
from pytket.passes import RebaseHQS # type: ignore
from pytket.extensions.honeywell import HoneywellBackend
from pytket.qasm import circuit_to_qasm_str


Expand All @@ -26,7 +26,7 @@ def test_convert() -> None:
circ.ZZPhase(0.3, 2, 3).CX(3, 0).Tdg(1)
circ.measure_all()

RebaseHQS().apply(circ)
HoneywellBackend("", login=False).rebase_pass().apply(circ)
circ_hqs = circuit_to_qasm_str(circ, header="hqslib1")
qasm_str = circ_hqs.split("\n")[6:-1]
test = True
Expand Down
3 changes: 2 additions & 1 deletion modules/pytket-ionq/pytket/extensions/ionq/backends/ionq.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
RenameQubitsPass,
SimplifyInitial,
)
from pytket._tket.circuit._library import _TK1_to_RzRx
from pytket.predicates import ( # type: ignore
GateSetPredicate,
MaxNQubitsPredicate,
Expand Down Expand Up @@ -186,7 +187,7 @@ def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
self.rebase_pass(),
SquashCustom(
ionq_singleqs,
lambda a, b, c: Circuit(1).Rz(c, 0).Rx(b, 0).Rz(a, 0),
_TK1_to_RzRx,
),
SimplifyInitial(allow_classical=False, create_all_qubits=True),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@

from typing import Dict, Tuple, Any, List
from pytket.passes import RebaseCustom # type: ignore
from pytket._tket.circuit._library import _TK1_to_RzRx # type: ignore
from pytket.circuit import Circuit, OpType, Command # type: ignore
from numpy import pi


def _from_tk1(a: float, b: float, c: float) -> Circuit:
circ = Circuit(1)
circ.Rz(c, 0)
circ.Rx(b, 0)
circ.Rz(a, 0)
return circ


ionq_multiqs = {
OpType.SWAP,
OpType.CX,
Expand Down Expand Up @@ -56,10 +49,9 @@ def _from_tk1(a: float, b: float, c: float) -> Circuit:
ionq_gates = ionq_multiqs.union(ionq_singleqs)

ionq_rebase_pass = RebaseCustom(
ionq_multiqs,
ionq_multiqs | ionq_singleqs,
Circuit(), # cx_replacement (irrelevant)
ionq_singleqs, # singleqs
_from_tk1,
_TK1_to_RzRx,
) # tk1_replacement

ionq_gate_dict = {
Expand Down
2 changes: 1 addition & 1 deletion modules/pytket-iqm/pytket/extensions/iqm/backends/iqm.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def _iqm_rebase() -> BasePass:
.add_gate(OpType.PhasedX, [1 + b, a], [0])
)

return RebaseCustom({OpType.CZ}, c_cx, {OpType.PhasedX}, c_tk1)
return RebaseCustom({OpType.CZ, OpType.PhasedX}, c_cx, c_tk1)


_xcirc = Circuit(1).add_gate(OpType.PhasedX, [1, 0], [0]).add_phase(0.5)
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
from pytket.backends.backendresult import BackendResult
from pytket.passes import ( # type: ignore
BasePass,
RebaseProjectQ,
SequencePass,
SynthesiseTket,
FullPeepholeOptimise,
Expand All @@ -63,7 +62,7 @@
Predicate,
)
from pytket.routing import Architecture # type: ignore
from pytket.extensions.projectq.projectq_convert import tk_to_projectq # type: ignore
from pytket.extensions.projectq.projectq_convert import tk_to_projectq, _REBASE # type: ignore
from pytket.extensions.projectq._metadata import __extension_version__ # type: ignore
from pytket.utils.operators import QubitPauliOperator
from pytket.utils.results import KwargTypes
Expand Down Expand Up @@ -134,7 +133,7 @@ def required_predicates(self) -> List[Predicate]:
]

def rebase_pass(self) -> BasePass:
return RebaseProjectQ()
return _REBASE

def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
assert optimisation_level in range(3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from projectq.types._qubit import Qureg # type: ignore
from pytket.circuit import OpType, Op, Circuit, Command, Bit # type: ignore
from pytket.transform import Transform # type: ignore
from pytket.passes import RebaseCustom # type: ignore
from pytket._tket.circuit._library import _CX, _TK1_to_RzRx # type: ignore
import numpy as np

_pq_to_tk_singleqs = {
Expand Down Expand Up @@ -56,7 +58,7 @@


_ALLOWED_GATES = {**_pq_to_tk_singleqs, **_pq_to_tk_multiqs, **_OTHER_KNOWN_GATES}

_REBASE = RebaseCustom(set(_ALLOWED_GATES.values()), _CX(), _TK1_to_RzRx)
_tk_to_pq_singleqs: dict = dict(
((item[1], item[0]) for item in _pq_to_tk_singleqs.items())
)
Expand Down Expand Up @@ -325,7 +327,7 @@ def _optimise(
# ProjectQ commands
if self._circuit.n_qubits != 0:
Transform.OptimisePhaseGadgets().apply(self._circuit)
Transform.RebaseToProjectQ().apply(self._circuit)
_REBASE.apply(self._circuit)

cmd_list = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
BasePass,
EulerAngleReduction,
CXMappingPass,
RebaseQuil,
auto_rebase_pass,
SequencePass,
SynthesiseTket,
DecomposeBoxes,
Expand Down Expand Up @@ -126,7 +126,7 @@ def required_predicates(self) -> List[Predicate]:
]

def rebase_pass(self) -> BasePass:
return RebaseQuil()
return auto_rebase_pass({OpType.CZ, OpType.Rz, OpType.Rx})

def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
assert optimisation_level in range(3)
Expand Down Expand Up @@ -339,7 +339,7 @@ def required_predicates(self) -> List[Predicate]:
]

def rebase_pass(self) -> BasePass:
return RebaseQuil()
return auto_rebase_pass({OpType.CZ, OpType.Rz, OpType.Rx})

def default_compilation_pass(self, optimisation_level: int = 1) -> BasePass:
assert optimisation_level in range(3)
Expand Down
20 changes: 3 additions & 17 deletions modules/pytket-qiskit/pytket/extensions/qiskit/backends/aer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
CXMappingPass,
DecomposeBoxes,
FullPeepholeOptimise,
RebaseCustom,
SequencePass,
SynthesiseTket,
auto_rebase_pass,
)
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.predicates import ( # type: ignore
Expand All @@ -57,8 +57,6 @@
)
from pytket.extensions.qiskit.qiskit_convert import (
tk_to_qiskit,
_qiskit_gates_1q,
_qiskit_gates_2q,
_gate_str_2_optype,
get_avg_characterisation,
)
Expand Down Expand Up @@ -90,15 +88,6 @@ def _default_q_index(q: Qubit) -> int:


_required_gates: Set[OpType] = {OpType.CX, OpType.U1, OpType.U2, OpType.U3}
_1q_gates: Set[OpType] = set(_qiskit_gates_1q.values())
_2q_gates: Set[OpType] = set(_qiskit_gates_2q.values())


def _tk1_to_u(a: float, b: float, c: float) -> Circuit:
circ = Circuit(1)
circ.add_gate(OpType.U3, [b, a - 0.5, c + 0.5], [0])
circ.add_phase(-0.5 * (a + c))
return circ


class _AerBaseBackend(Backend):
Expand Down Expand Up @@ -143,11 +132,8 @@ def backend_info(self) -> BackendInfo:
return self._backend_info

def rebase_pass(self) -> BasePass:
return RebaseCustom(
self._gate_set & _2q_gates,
Circuit(2).CX(0, 1),
self._gate_set & _1q_gates,
_tk1_to_u,
return auto_rebase_pass(
self._gate_set,
)

def process_circuits(
Expand Down
Loading

0 comments on commit 94f67c1

Please sign in to comment.