# 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': 105.087
• 'Y': 81.282
• 'Z': 17.215
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', -110.89189624501294), ('X', 90), ('Z', 48.223321798518754), ('X', 90), ('Z', -35.44956525321924)]


## 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(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': -148.468 'Y': 106.714 'Z': 51.023 Matrix: -0.27 -0.07j 0.35 0.89j 0.96 -0.05j 0.18 0.22j (1): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': 2.421 'Y': 19.819 'Z': -69.748 Matrix: -0.13 0.98j -0.06 -0.15j 0.09 0.13j 0.88 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: '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': -155.59 'Y': -0.706 'Z': 55.909 Matrix: -0.29 -0.21j 0.90 0.25j 0.90 0.25j 0.36 -0.03j (1): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': -73.345 'Y': -58.782 'Z': -156.683 Matrix: 0.28 0.81j 0.44 0.26j -0.16 0.49j -0.32 -0.79j 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': -2.787 'Y': -103.925 'Z': 143.133 Matrix: 0.43 -0.68j 0.50 0.31j -0.51 -0.28j -0.39 0.71j (1): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': 43.41 'Y': 130.881 'Z': -32.311 Matrix: 0.15 0.36j -0.56 -0.73j 0.88 0.25j 0.39 0.01j 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': 6.295 'Y': 152.905 'Z': -34.54 Matrix: 0.21 -0.21j 0.01 0.96j -0.09 -0.95j -0.23 -0.19j (1): Gate(X, Y, ...) Name: Gate(X, Y, ...) Generators: 'X': 55.775 'Y': -120.955 'Z': 182.215 Matrix: -0.72 -0.44j 0.31 -0.45j -0.54 0.06j 0.05 0.84j

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. 