I have a specific basis gate set for my system, is it possible to use pytket's predefined optimization sequences and ask it to give a final circuit using my specific gate set?
1 Answers
Yes it is possible to get pytket to give you a circuit in the desired gateset.
The simple case
In certain cases this can be done automatically with the AutoRebase. This auto rebase depends on a known set of hardcoded gate decompositions specified here. If no suitable decomposition is found for your target gateset then you will need to define you rebase manually with RebaseCustom.
Typically if your gateset is one used by a hardware provider (i.e. IBM's {X, SX, CX, Rz} gateset) Then AutoRebase will work.
from pytket import Circuit, OpType
from pytket.passes import AutoRebase
circ1 = Circuit(2).H(0).Rx(0.25, 1).CZ(0, 1) # test circuit
ibm_gateset = {OpType.X, OpType.SX, OpType.CX, OpType.Rz} # Target gateset
ibm_rebase = AutoRebase(ibm_gateset) # Define rebase
ibm_rebase.apply(circ1) # Apply rebase pass
More involved case with RebaseCustom
Works for any gateset for which suitable replacements are given.
Below is an example of a custom rebase taken from the user manual. Note that in addition to the target gateset I need to provide replacements to convert the gates in my circuit to TKET's internal gateset {TK1, CX}. {TK1, TK2} is also allowed. See the gate definitions.
from pytket import Circuit, OpType
from pytket.passes import RebaseCustom
from pytket.circuit.display import render_circuit_jupyter
Target gateset
gates = {OpType.Rz, OpType.Ry, OpType.CY, OpType.ZZPhase}
Implement CX in terms of CY, Rz
cx_in_cy = Circuit(2)
cx_in_cy.Rz(0.5, 1).CY(0, 1).Rz(-0.5, 1)
Define function to implement TK1 gate in terms of Rz and Ry
def tk1_to_rzry(a, b, c):
circ = Circuit(1)
circ.Rz(c + 0.5, 0).Ry(b, 0).Rz(a - 0.5, 0)
return circ
Define rebase
custom_rebase = RebaseCustom(gates, cx_in_cy, tk1_to_rzry)
circ2 = Circuit(3) # Test circuit
circ2.X(0).CX(0, 1).Ry(0.2, 1)
circ2.ZZPhase(-0.83, 2, 1).Rx(0.6, 2)
custom_rebase.apply(circ2) # apply custom transformation
render_circuit_jupyter(circ2)
Combining passes
If you want to perform the gate conversion after you've applied some optimisations then you can include your rebase at the end of a SequencePass and the different circuit transformations will be performed in order.
from pytket.passes import SequencePass, FullPeepholeOptimise
circ3 = Circuit(2).H(0).Rx(0.25, 1).CZ(0, 1).CX(1, 0).CY(0, 1)
seq_pass = SequencePass([FullPeepholeOptimise(), ibm_rebase])
Apply FullPeepholeOptimise followed by gateset rebase
seq_pass.apply(circ)
I hope this helps. We hope to have a more intuitive interface for gate conversion in future releases.
- 1,260
- 1
- 5
- 24