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(
    """
    Name: Example
    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 patterns for the transpiler so it will use the DEFAULT_PATTERNS, 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_PATTERNS.

# Make a transpiler based on the device configuration, with default patterns:
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.
 
imm
(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.
   
imm
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • -0.95 0.32j 0.32 0.95j
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • -0.95 0.32j 0.32 0.95j
imm
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
imm
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = 180.0
Generators:
  • 'Z': -180.0
Matrix:
  • 0.80 0.60j -0.80 -0.60j
 
imm
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
imm
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • -0.95 0.32j 0.32 0.95j
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • -0.95 0.32j 0.32 0.95j
imm
(0, 1): Example.CNOT()
Name:
  • Example.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
 
imm
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • -0.95 0.32j 0.32 0.95j
 
imm
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
imm
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • -0.95 -0.32j 0.32 -0.95j
 
imm
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
imm
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = -180.0
Generators:
  • 'Z': 180.0
Matrix:
  • 0.80 -0.60j -0.80 0.60j
 
imm
(0, 1): Example.CNOT()
Name:
  • Example.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
 
imm
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • -0.95 0.32j 0.32 0.95j
 
imm
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
imm
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • -0.95 -0.32j 0.32 -0.95j
 
imm
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
imm
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = -180.0
Generators:
  • 'Z': 180.0
Matrix:
  • 0.80 -0.60j -0.80 0.60j
 
imm
(0, 1): Example.CNOT()
Name:
  • Example.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
 
imm
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • -0.95 -0.32j 0.32 -0.95j
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • -0.95 -0.32j 0.32 -0.95j
imm
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
imm
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • -0.95 -0.32j 0.32 -0.95j
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • -0.95 -0.32j 0.32 -0.95j
imm
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90.0
Generators:
  • 'X': 90.0
Matrix:
  • -0.32 -0.63j -0.63 0.32j -0.63 0.32j -0.32 -0.63j
imm
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • -0.95 0.32j 0.32 0.95j
(1): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • -0.95 0.32j 0.32 0.95j


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 be a complex global
# constant offset from the ideal matrix.
tq.visualization.plot_mat(matrix)
compiler

Out:

Process Infidelity:  4.884981308350689e-15

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

Gallery generated by Sphinx-Gallery