Compiler: Converting to a Gateset

In this example, we show how to transpile a circuit to contain only a device’s native gateset. This native gateset is defined using a Config object which, below, contains only rotations about \(Z\) or \(X\) and a CNOT gate. This example contains a relatively simple configuration; they can generally be more complex.

import trueq as tq

# Define a device configuration
config = tq.Config.from_yaml(
    """
    Mode: ZXZXZ
    Gate Z:
        Hamiltonian:
        - ['Z', 'phi']
    Gate X:
        Hamiltonian:
        - ['X', 'phi']
    Gate CNOT:
        Matrix:
        - [1, 0, 0, 0]
        - [0, 1, 0, 0]
        - [0, 0, 0, 1]
        - [0, 0, 1, 0]
    """
)

Next, we define a compiler based on the Config object we created above. Note that in this instance, we do not specify passes for the transpiler so it will use the DEFAULT_PASSES, which are given in compilation. If this compiler is applied to a circuit, it will translate that circuit into a new circuit composed of gates contained in the config, so that it can be implemented on our imaginary device. The new circuit will be simplified according to DEFAULT_PASSES.

# Make a transpiler based on the device configuration, with default passes:
transpiler = tq.compilation.get_transpiler(config)

Consider the following circuit that contains the non-native SWAP gate.

# Define a circuit
circuit = tq.Circuit([{(0, 1): tq.Gate.swap}])
circuit
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Circuit
Key:
No key present in circuit.
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate.swap
Name:
  • Gate.swap
Aliases:
  • Gate.swap
Likeness:
  • SWAP
Generators:
  • 'XX': 90.0
  • 'YY': 90.0
  • 'ZZ': 90.0
Matrix:
  • 1.00 1.00 1.00 1.00


Using the transpiler, we can convert it into a new circuit. Notice that all of the gates in the new circuit are from the Config object, and that single-qubit gates are decomposed into sequences of the form \(Z(\theta)X(90)Z(\phi)X(90)Z(\gamma)\), which is consistent with the mode in the configuration. Hovering your cursor over each gate will display the rotation angle.

transpiled_circuit = transpiler.compile(circuit)
transpiled_circuit
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Circuit
Key:
No key present in circuit.
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Z(phi)
Name:
  • Z(phi)
Parameters:
  • phi = -135.0
Generators:
  • 'Z': -135.0
Matrix:
  • 0.38 0.92j 0.38 -0.92j
(1): Z(phi)
Name:
  • Z(phi)
Parameters:
  • phi = -45.0
Generators:
  • 'Z': -45.0
Matrix:
  • 0.92 0.38j 0.92 -0.38j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = -270.0
Generators:
  • 'Z': -270.0
Matrix:
  • -0.71 0.71j -0.71 -0.71j
(1): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): CNOT()
Name:
  • CNOT()
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'IX': 90.0
  • 'ZI': 90.0
  • 'ZX': -90.0
Matrix:
  • 1.00 1.00 1.00 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(1): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = -180.0
Generators:
  • 'Z': -180.0
Matrix:
  • 1.00j -1.00j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): CNOT()
Name:
  • CNOT()
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'IX': 90.0
  • 'ZI': 90.0
  • 'ZX': -90.0
Matrix:
  • 1.00 1.00 1.00 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(1): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(1): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = -180.0
Generators:
  • 'Z': -180.0
Matrix:
  • 1.00j -1.00j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): CNOT()
Name:
  • CNOT()
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'IX': 90.0
  • 'ZI': 90.0
  • 'ZX': -90.0
Matrix:
  • 1.00 1.00 1.00 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(1): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(1): Z(phi)
Name:
  • Z(phi)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = 180.0
Generators:
  • 'Z': 180.0
Matrix:
  • -1.00j 1.00j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): X(phi)
Name:
  • X(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Z(phi)
Name:
  • Z(phi)
Parameters:
  • phi = -135.0
Generators:
  • 'Z': -135.0
Matrix:
  • 0.38 0.92j 0.38 -0.92j
(1): Z(phi)
Name:
  • Z(phi)
Parameters:
  • phi = -45.0
Generators:
  • 'Z': -45.0
Matrix:
  • 0.92 0.38j 0.92 -0.38j


We can verify that this circuit makes the SWAP gate using an ideal simulator:

matrix = tq.Simulator().operator(transpiled_circuit).mat()

print("Process Infidelity: ", tq.math.proc_infidelity(matrix, tq.Gate.swap.mat))

# Note that the compiler does not preserve global phase, so there may be a complex
# global constant offset from the ideal matrix.
tq.visualization.plot_mat(matrix)
compiler

Out:

Process Infidelity:  8.881784197001252e-16

Total running time of the script: ( 0 minutes 0.090 seconds)

Gallery generated by Sphinx-Gallery