# 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(X, Y, ...)
Generators:
• 'X': 82.796
• 'Y': 88.468
• 'Z': 108.998
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


Out:

[('Z', -59.50853615719257), ('X', 90), ('Z', 85.33967214233539), ('X', 90), ('Z', 34.285452125296075)]


## 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_su4(
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(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': 154.189 'Y': 116.869 'Z': 114.391 Matrix: -0.12 -0.59j -0.13 -0.78j 0.72 -0.34j -0.56 0.24j (1): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': -64.617 'Y': 66.121 'Z': 187.73 Matrix: 0.31 -0.85j -0.42 0.06j 0.07 0.42j -0.72 0.55j Marker 0 Compilation tools may only recompile cycles with equal markers. (0, 1): Gate.iswap Name: Gate.iswap Aliases: Gate.iswap Likeness: iSWAP Generators: 'XX': -90.0 'YY': -90.0 Matrix: 1.00 1.00j 1.00j 1.00 Marker 0 Compilation tools may only recompile cycles with equal markers. (0): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': -82.151 'Y': -73.414 'Z': 110.117 Matrix: 0.68 -0.24j -0.14 0.68j -0.69 -0.06j -0.43 0.58j (1): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': -178.154 'Y': 158.521 'Z': -64.842 Matrix: -0.54 0.24j -0.51 0.62j 0.56 0.58j -0.56 -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: 'XX': -90.0 'YY': -90.0 Matrix: 1.00 1.00j 1.00j 1.00 Marker 0 Compilation tools may only recompile cycles with equal markers. (0): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': -136.916 'Y': 93.599 'Z': -88.491 Matrix: -0.01 0.47j -0.39 0.79j 0.59 0.65j -0.13 -0.46j (1): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': 85.006 'Y': -98.334 'Z': -190.148 Matrix: -0.03 0.86j 0.19 -0.47j -0.50 -0.12j -0.72 -0.47j Marker 0 Compilation tools may only recompile cycles with equal markers. (0, 1): Gate.iswap Name: Gate.iswap Aliases: Gate.iswap Likeness: iSWAP Generators: 'XX': -90.0 'YY': -90.0 Matrix: 1.00 1.00j 1.00j 1.00 Marker 0 Compilation tools may only recompile cycles with equal markers. (0): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': 39.813 'Y': -183.654 'Z': -25.308 Matrix: -0.16 0.00j 0.71 0.69j -0.36 -0.92j 0.06 -0.14j (1): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': 158.427 'Y': 51.974 'Z': -14.735 Matrix: 0.07 0.12j 0.02 -0.99j 0.60 -0.79j 0.13 -0.05j

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. 