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?

  • $\begingroup$ Hi modibrah, I'd be happy to answer your question. I assume by pre-defined sequences you mean passes like FullPeepholeOptimise or the default_compilation_pass for a specific Backend? $\endgroup$
    – Callum
    Commented Mar 6, 2023 at 12:36
  • $\begingroup$ @Callum Yes, exactly! Thank you $\endgroup$
    – mondibrah
    Commented Mar 6, 2023 at 18:20
  • 1
    $\begingroup$ Does my answer below address your question properly? $\endgroup$
    – Callum
    Commented Mar 6, 2023 at 18:33

1 Answer 1


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 auto_rebase_pass. 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 auto_rebase_pass will work.

from pytket import Circuit, OpType
from pytket.passes import auto_rebase_pass

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 = auto_rebase_pass(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.add_gate(OpType.ZZPhase, -0.83, [2, 1]).Rx(0.6, 2)

custom_rebase.apply(circ2) # apply custom transformation


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

I hope this helps. We hope to have a more intuitive interface for gate conversion in future releases.

  • $\begingroup$ Thank you! This answers my question perfectly! I will start working on a base code and come back to this thread if I faced any problems! $\endgroup$
    – mondibrah
    Commented Mar 7, 2023 at 20:31
  • $\begingroup$ Happy to help :) $\endgroup$
    – Callum
    Commented Mar 7, 2023 at 21:38
  • $\begingroup$ Also, I'm following the steps here link to create a custom gate (Rxy). Once I define the gate, how do I include it in the set gates = {OpType.Rz, OpType.CZ, etc}? $\endgroup$
    – mondibrah
    Commented Mar 15, 2023 at 10:35
  • $\begingroup$ The solution was to define my gate as shown in the documentation, and use OpType.CustomGate in the gates list $\endgroup$
    – mondibrah
    Commented Mar 16, 2023 at 23:59
  • $\begingroup$ Yes this should work :) Let me know if there are further issues $\endgroup$
    – Callum
    Commented Mar 17, 2023 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.