# Example: 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': 121.927
• 'X': 84.562
• 'Z': -111.051
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', -239.70358102766073),
('X', 90),
('Z', 73.78934886465902),
('X', 90),
('Z', -129.18957320035088)]


## 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': -39.915 'X': -107.191 'Z': -42.005 Matrix: -0.24 0.52j -0.73 0.38j -0.80 -0.19j 0.36 0.45j (1): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': 106.399 'X': -187.142 'Z': -128.224 Matrix: -0.41 0.58j -0.13 0.69j 0.53 0.46j -0.68 -0.20j 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': 69.523 'X': -94.594 'Z': -243.065 Matrix: -0.39 0.87j -0.07 0.30j 0.27 0.15j -0.90 -0.30j (1): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': 164.075 'X': 54.822 'Z': 88.433 Matrix: 0.12 -0.45j -0.59 -0.66j 0.87 0.17j -0.33 0.33j 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': 116.477 'X': -3.951 'Z': 6.082 Matrix: 0.16 0.50j -0.22 -0.82j 0.16 0.83j 0.07 0.52j (1): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': 64.58 'X': -71.542 'Z': 21.932 Matrix: -0.04 0.67j -0.37 -0.64j -0.68 0.30j -0.36 0.57j 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': -6.86 'X': 64.633 'Z': 97.594 Matrix: -0.78 -0.41j -0.47 0.02j -0.46 0.12j 0.63 -0.62j (1): Gate(Y, X, ...) Name: Gate(Y, X, ...) Generators: 'Y': -89.612 'X': -50.837 'Z': 109.75 Matrix: -0.69 -0.30j 0.36 -0.56j 0.29 0.59j 0.72 -0.21j

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. 