# Gate synthesis

This example demonstrates how the built-in compiler can be used to perform gate synthesis.

## Synthesizing Single-Qubit Gates

We begin by generating a random gate in $SU(2)$; we will synthesize this gate later.

import trueq as tq

# create a Haar random SU(2) gate and print its matrix representation
U = tq.Gate.random(2)
U

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".
Name:
• Gate(Y, X, ...)
Generators:
• 'Y': -51.746
• 'X': 130.106
• 'Z': 82.858
Matrix:

We can perform single qubit gate decomposition into a number of possible “modes”. Here we decompose the gate U into a ZXZXZ decomposition, which is short hand for $Z(\theta)X(90)Z(\phi)X(90)Z(\gamma)$. See QubitMode for a complete list of all available single qubit decompositions.

synthesized_gate = tq.math.QubitMode.ZXZXZ.decompose(U)

# print the synthesized gate as a list of single-qubit rotations about Z and X
synthesized_gate

[('Z', 5.055426195845186), ('X', 90), ('Z', 63.400037535166874), ('X', 90), ('Z', -38.32237979942232)]


## Synthesizing Two-Qubit Gates

In the event that we want to express a two-qubit gate in terms of a different two-qubit gate, we can use the compiler to synthesize the desired gate. Here we decompose a random $SU(4)$ operation so that it can be implemented using iSWAP gates.

# define the gate to be synthesized
gate_to_be_synthesized = tq.Gate.random(4)

# re-express the gate using an iswap gate as the two-qubit gate
two_qubit_synthesized_gate = tq.math.decompose_unitary(
target_gate=gate_to_be_synthesized, given_gate=tq.Gate.iswap
)

# print the synthesized gate
two_qubit_synthesized_gate

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(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': -54.95 'X': 59.513 'Z': 39.999 Matrix: -0.21 -0.74j -0.40 -0.49j -0.52 0.36j 0.41 -0.65j (1): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': -34.177 'X': -13.947 'Z': 109.461 Matrix: 0.64 0.72j -0.16 0.22j -0.04 -0.27j -0.91 0.31j Marker 0 Compilation tools may only recompile cycles with equal markers. (0, 1): Gate.iswap Name: Gate.iswap Aliases: Gate.iswap Likeness: iSWAP Generators: 'YY': -90.0 'XX': -90.0 Matrix: 1.00 1.00j 1.00j 1.00 Marker 0 Compilation tools may only recompile cycles with equal markers. (0): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': -10.137 'X': 30.427 'Z': -31.955 Matrix: 0.76 -0.59j -0.16 -0.22j -0.26 -0.08j 0.32 -0.91j (1): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': 46.073 'X': -235.223 'Z': -109.872 Matrix: -0.72 0.16j -0.27 0.62j -0.02 0.68j -0.58 -0.45j Marker 0 Compilation tools may only recompile cycles with equal markers. (0, 1): Gate.iswap Name: Gate.iswap Aliases: Gate.iswap Likeness: iSWAP Generators: 'YY': -90.0 'XX': -90.0 Matrix: 1.00 1.00j 1.00j 1.00 Marker 0 Compilation tools may only recompile cycles with equal markers. (0): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': 50.338 'X': -48.48 'Z': 114.784 Matrix: 0.65 0.58j -0.23 -0.42j -0.41 0.24j -0.86 0.16j (1): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': -194.227 'X': -36.733 'Z': 192.102 Matrix: -0.70 -0.53j 0.46 0.13j -0.48 0.05j -0.78 0.41j Marker 0 Compilation tools may only recompile cycles with equal markers. (0, 1): Gate.iswap Name: Gate.iswap Aliases: Gate.iswap Likeness: iSWAP Generators: 'YY': -90.0 'XX': -90.0 Matrix: 1.00 1.00j 1.00j 1.00 Marker 0 Compilation tools may only recompile cycles with equal markers. (0): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': 125.339 'X': 123.405 'Z': -68.108 Matrix: -0.21 0.30j -0.35 -0.86j 0.87 -0.34j 0.07 -0.36j (1): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': -101.816 'X': -47.421 'Z': 52.2 Matrix: -0.43 -0.42j 0.24 -0.76j 0.43 0.67j 0.31 -0.52j

This circuit can be verified to reproduce the original random unitary using an ideal simulator:

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

# This will result in an identity gate up to a global complex phase.