External Interfaces: Introduction

In this example, we demonstrate how to convert between the True-Q™ python circuit representation and other popular third party representations. These conversions will only be available if the corresponding python packages are installed.

We start by defining a test circuit to work with:

import trueq as tq

circuit = tq.Circuit(
    [
        {(0,): tq.Gate.x, (1,): tq.Gate.y},
        {(0, 2): tq.Gate.cz, (1,): tq.Gate.x},
        {(3,): tq.Gate.h, (5,): tq.Gate.s},
    ]
)
circuit.measure_all()
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): Gate.x
Name:
  • Gate.x
Aliases:
  • Gate.x
  • Gate.cliff1
Generators:
  • 'X': 180.0
Matrix:
  • 1.00 1.00
(1): Gate.y
Name:
  • Gate.y
Aliases:
  • Gate.y
  • Gate.cliff2
Generators:
  • 'Y': 180.0
Matrix:
  • -1.00j 1.00j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 2): Gate.cz
Name:
  • Gate.cz
Aliases:
  • Gate.cz
Likeness:
  • CNOT
Generators:
  • 'IZ': 90.0
  • 'ZI': 90.0
  • 'ZZ': -90.0
Matrix:
  • 1.00 1.00 1.00 -1.00
(1): Gate.x
Name:
  • Gate.x
Aliases:
  • Gate.x
  • Gate.cliff1
Generators:
  • 'X': 180.0
Matrix:
  • 1.00 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(3): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
(5): Gate.s
Name:
  • Gate.s
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Generators:
  • 'Z': 90.0
Matrix:
  • 1.00 1.00j
 
1
Marker 1
Compilation tools may only recompile cycles with equal markers.
(0): Meas()
Name:
  • Meas()
(1): Meas()
Name:
  • Meas()
(2): Meas()
Name:
  • Meas()
(3): Meas()
Name:
  • Meas()
(5): Meas()
Name:
  • Meas()


Cirq

Convert the circuit to a Cirq circuit object:

cirq_circuit = circuit.to_cirq()
cirq_circuit
(0, 0): ───H^0───X───H^0───@─────────────────────M───
                           │                     │
(1, 0): ───────────────────┼───H^0───H^0─────Z───M───
                           │                     │
(2, 0): ───────────────────@─────────────────────M───
                                                 │
(3, 0): ───────────────────────S─────X^0.5───S───M───
                                                 │
(5, 0): ───────────────────────H^0───H^0─────S───M───


Perform the inverse conversion (to_trueq() is monkey-patched onto their circuit object at True-Q™ import time):

cirq_circuit.to_trueq()
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.
1
Marker 1
Compilation tools may only recompile cycles with equal markers.
(0): Gate.id
Name:
  • Gate.id
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
 
2
Marker 2
Compilation tools may only recompile cycles with equal markers.
(0): Gate.x
Name:
  • Gate.x
Aliases:
  • Gate.x
  • Gate.cliff1
Generators:
  • 'X': 180.0
Matrix:
  • 1.00 1.00
 
3
Marker 3
Compilation tools may only recompile cycles with equal markers.
(0): Gate.id
Name:
  • Gate.id
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
 
4
Marker 4
Compilation tools may only recompile cycles with equal markers.
(0, 2): Gate.cz
Name:
  • Gate.cz
Aliases:
  • Gate.cz
Likeness:
  • CNOT
Generators:
  • 'IZ': 90.0
  • 'ZI': 90.0
  • 'ZZ': -90.0
Matrix:
  • 1.00 1.00 1.00 -1.00
 
5
Marker 5
Compilation tools may only recompile cycles with equal markers.
(1): Gate.id
Name:
  • Gate.id
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(3): Gate.s
Name:
  • Gate.s
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Generators:
  • 'Z': 90.0
Matrix:
  • 1.00 1.00j
(5): Gate.id
Name:
  • Gate.id
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
 
6
Marker 6
Compilation tools may only recompile cycles with equal markers.
(1): Gate.id
Name:
  • Gate.id
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(3): Gate.sx
Name:
  • Gate.sx
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.50 0.50j 0.50 -0.50j 0.50 -0.50j 0.50 0.50j
(5): Gate.id
Name:
  • Gate.id
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
 
7
Marker 7
Compilation tools may only recompile cycles with equal markers.
(1): Gate.z
Name:
  • Gate.z
Aliases:
  • Gate.z
  • Gate.cliff3
Generators:
  • 'Z': 180.0
Matrix:
  • 1.00 -1.00
(3): Gate.s
Name:
  • Gate.s
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Generators:
  • 'Z': 90.0
Matrix:
  • 1.00 1.00j
(5): Gate.s
Name:
  • Gate.s
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Generators:
  • 'Z': 90.0
Matrix:
  • 1.00 1.00j
 
8
Marker 8
Compilation tools may only recompile cycles with equal markers.
(0): Meas()
Name:
  • Meas()
(1): Meas()
Name:
  • Meas()
(2): Meas()
Name:
  • Meas()
(3): Meas()
Name:
  • Meas()
(5): Meas()
Name:
  • Meas()


PyQuil

Convert the circuit to a PyQuil circuit object:

pyquil_circuit = circuit.to_pyquil()
print(pyquil_circuit)

Out:

DECLARE ro BIT[5]
PHASE(-1.5707963267948968) 0
RX(1.5707963267948968) 0
I 0
RX(1.5707963267948968) 0
PHASE(-1.5707963267948968) 0
CZ 0 2
PHASE(-1.5707963267948968) 1
I 3
PHASE(-1.5707963267948968) 5
RX(1.5707963267948968) 1
RX(1.5707963267948968) 3
RX(1.5707963267948968) 5
Z 1
S 3
Z 5
RX(1.5707963267948968) 1
RX(1.5707963267948968) 3
RX(1.5707963267948968) 5
S 1
I 3
I 5
FENCE
MEASURE 0 ro[0]
MEASURE 1 ro[1]
MEASURE 2 ro[2]
MEASURE 3 ro[3]
MEASURE 5 ro[4]

Perform the inverse conversion (to_trueq() is monkey-patched onto their circuit object at True-Q™ import time):

pyquil_circuit.to_trueq()
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): PHASE(phi)
Name:
  • PHASE(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -1.570796
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): RX(phi)
Name:
  • RX(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 1.570796
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): PHASE(phi)
Name:
  • PHASE(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -1.570796
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(3): I()
Name:
  • I()
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(5): PHASE(phi)
Name:
  • PHASE(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -1.570796
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): I()
Name:
  • I()
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(1): RX(phi)
Name:
  • RX(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 1.570796
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): RX(phi)
Name:
  • RX(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 1.570796
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(5): RX(phi)
Name:
  • RX(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 1.570796
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): RX(phi)
Name:
  • RX(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 1.570796
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Z()
Name:
  • Z()
Aliases:
  • Gate.z
  • Gate.cliff3
Generators:
  • 'Z': 180.0
Matrix:
  • 1.00 -1.00
(3): S()
Name:
  • S()
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Generators:
  • 'Z': 90.0
Matrix:
  • 1.00 1.00j
(5): Z()
Name:
  • Z()
Aliases:
  • Gate.z
  • Gate.cliff3
Generators:
  • 'Z': 180.0
Matrix:
  • 1.00 -1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): PHASE(phi)
Name:
  • PHASE(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -1.570796
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(1): RX(phi)
Name:
  • RX(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 1.570796
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): RX(phi)
Name:
  • RX(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 1.570796
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(5): RX(phi)
Name:
  • RX(phi)
Aliases:
  • Gate.sx
  • Gate.cliff5
Parameters:
  • phi = 1.570796
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, 2): CZ()
Name:
  • CZ()
Aliases:
  • Gate.cz
Likeness:
  • CNOT
Generators:
  • 'IZ': 90.0
  • 'ZI': 90.0
  • 'ZZ': -90.0
Matrix:
  • 1.00 1.00 1.00 -1.00
(1): S()
Name:
  • S()
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Generators:
  • 'Z': 90.0
Matrix:
  • 1.00 1.00j
(3): I()
Name:
  • I()
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(5): I()
Name:
  • I()
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
 
1
Marker 1
Compilation tools may only recompile cycles with equal markers.
(0): Meas()
Name:
  • Meas()
(1): Meas()
Name:
  • Meas()
(2): Meas()
Name:
  • Meas()
(3): Meas()
Name:
  • Meas()
(5): Meas()
Name:
  • Meas()


Qiskit

Set the preferred gates to build into (See below for more details), then convert the circuit to a Qiskit circuit object:

tq.interface.qiskit.set_config(["CXGate", "UGate"])
qiskit_circuit = circuit.to_qiskit()
qiskit_circuit.draw("mpl")
interface software

Out:

Warning: The qiskit.circuit.library.standard_gates.ms import is deprecated as of 0.16.0. You should import MSGate using qiskit.circuit.library.generalized_gates instead.
         (/home/user/jenkins/workspace/release trueq/trueq/interface/qiskit_interface.py:222)

<Figure size 872.774x618.722 with 1 Axes>

Perform the inverse conversion (to_trueq() is monkey-patched onto their circuit object at True-Q™ import time):

qiskit_circuit.to_trueq()
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): UGate(theta, phi, ...)
Name:
  • UGate(theta, phi, ...)
Aliases:
  • Gate.cliff11
Parameters:
  • theta = 3.141593
  • phi = 1.570796
  • lam = 0.0
Generators:
  • 'X': -127.279
  • 'Y': 127.279
Matrix:
  • -0.71 0.71j 0.71 0.71j
(2): UGate(theta, phi, ...)
Name:
  • UGate(theta, phi, ...)
Aliases:
  • Gate.cliff23
Parameters:
  • theta = 1.570796
  • phi = 0.0
  • lam = 1.570796
Generators:
  • 'X': 69.282
  • 'Y': 69.282
  • 'Z': 69.282
Matrix:
  • 0.50 -0.50j -0.50 -0.50j 0.50 -0.50j 0.50 0.50j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 2): CXGate()
Name:
  • CXGate()
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): UGate(theta, phi, ...)
Name:
  • UGate(theta, phi, ...)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • theta = 0.0
  • phi = 1.570796
  • lam = 0.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): UGate(theta, phi, ...)
Name:
  • UGate(theta, phi, ...)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • theta = 0.0
  • phi = -3.141593
  • lam = 0.0
Generators:
  • 'Z': -180.0
Matrix:
  • 1.00j -1.00j
(2): UGate(theta, phi, ...)
Name:
  • UGate(theta, phi, ...)
Aliases:
  • Gate.cliff16
Parameters:
  • theta = 1.570796
  • phi = 1.570796
  • lam = 3.141593
Generators:
  • 'X': 138.564
  • 'Y': 138.564
  • 'Z': 138.564
Matrix:
  • -0.50 -0.50j -0.50 -0.50j 0.50 -0.50j -0.50 0.50j
(3): UGate(theta, phi, ...)
Name:
  • UGate(theta, phi, ...)
Aliases:
  • Gate.h
  • Gate.cliff12
Parameters:
  • theta = 1.570796
  • phi = 0.0
  • lam = 3.141593
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • -0.71j -0.71j -0.71j 0.71j
(5): UGate(theta, phi, ...)
Name:
  • UGate(theta, phi, ...)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • theta = 0.0
  • phi = 1.570796
  • lam = 0.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
1
Marker 1
Compilation tools may only recompile cycles with equal markers.
(0): Meas()
Name:
  • Meas()
(1): Meas()
Name:
  • Meas()
(2): Meas()
Name:
  • Meas()
(3): Meas()
Name:
  • Meas()
(5): Meas()
Name:
  • Meas()


Interface Config

During conversion between representations in True-Q™ and other software packages, there is often a degeneracy in how a gate may be converted. For example, an identity gate could be written as Z(phi), X(phi), Y(phi), … with phi = 0.

In these cases, this degeneracy can be broken by setting the preferred order of gate conversion. This is done in the interfaces through a Config which contains a collection of GateFactorys. Each of these factories has the exact name and parameters of the corresponding gate representation in the external software. During the conversion of any specific gate from True-Q™ into the external software, each of these factories is used in order until the conversion is possible.

The default settings for all interfaces is to have the defined config contain all possible gates allowed in the external software, even though there are typically degeneracies. This default configuration is available through the default_config() method on every interface.

The current active config may be changed through the respective set_config() and get_config() which are also available on every interface.

Look at the config list for the interface between True-Q™ and Cirq:

std_config = tq.interface.cirq.default_config()

Set a reduced config, containing only CNOT, XPowGate, and ZPowGate:

tq.interface.cirq.set_config(["CNOT", "XPowGate", "ZPowGate"])

This may have also been set through a subset of the default config:

tq.interface.cirq.set_config(std_config.subset(names=["CNOT", "XPowGate", "ZPowGate"]))

Convert the circuit defined above to Cirq using the updated config:

circuit.to_cirq()
(0, 0): ───X^0───X───────S^-1───@───X^0───X^0─────S───M───
                                │                     │
(1, 0): ────────────────────────┼───X^0───X^0─────Z───M───
                                │                     │
(2, 0): ───X^0───X^0.5───S──────X───S─────X^0.5───Z───M───
                                                      │
(3, 0): ────────────────────────────S─────X^0.5───S───M───
                                                      │
(5, 0): ────────────────────────────X^0───X^0─────S───M───


Note

Setting the config to None resets to the default config.

Reset the config to the default:

tq.interface.cirq.set_config(None)

Convert the circuit to Cirq using the default config

circuit.to_cirq()
(0, 0): ───H^0───X───H^0───@─────────────────────M───
                           │                     │
(1, 0): ───────────────────┼───H^0───H^0─────Z───M───
                           │                     │
(2, 0): ───────────────────@─────────────────────M───
                                                 │
(3, 0): ───────────────────────S─────X^0.5───S───M───
                                                 │
(5, 0): ───────────────────────H^0───H^0─────S───M───


As you can see in the printed circuits, changing the config alters the gates used in converting from True-Q™ to other software. For example, removing the X gate from the config causes the interface to pick the next viable representation of this operation, so that the X gates are given as rotations about X by \(\pi\).

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

Gallery generated by Sphinx-Gallery