pygridsynth is a Python library for approximating arbitrary Z-rotations using the Clifford+T gate set, based on a given angle θ and tolerance ε. It is particularly useful for addressing approximate gate synthesis problems in quantum computing and algorithm research.
Features
Inspired by Established Work: This library is based on P. Selinger's gridsynth program (newsynth), adapted for Python with additional functionality.
High Precision: Utilizes the mpmath library to support high-precision calculations.
Customizable: Allows adjustment of calculation precision (dps) and verbosity of output.
Graph Visualization: Provides an option to visualize decomposition results as a graph.
--showgraph, -g: Displays the decomposition result as a graph.
--up-to-phase, -ph: Approximates up to a phase.
Example Execution
pygridsynth 0.5 1e-50 --verbose --time
This command will:
Compute the Clifford+T gate decomposition of a Z-rotation gate $R_Z(\theta) = e^{- i \frac{\theta}{2} Z}$ with $\theta = 0.5$ and $\epsilon = 10^{-50}$.
Display detailed output and measure the execution time.
Using as a Library
You can also use pygridsynth directly in your scripts:
The recommended way is to use mpmath.mpf to ensure high-precision calculations:
It is also possible to use floats, but beware that floating-point numbers may introduce precision errors.
Floats should only be used when high precision is not required:
import numpy as np
from pygridsynth.gridsynth import gridsynth_gates
# Float can be used if high precision is not required
theta = 0.5
epsilon = 1e-10
gates = gridsynth_gates(theta=theta, epsilon=epsilon)
print(gates)
theta = float(np.pi / 8)
epsilon = 1e-10
gates = gridsynth_gates(theta=theta, epsilon=epsilon)
print(gates)
Multi-Qubit Unitary Approximation
pygridsynth provides functionality for approximating multi-qubit unitary matrices using the Clifford+T gate set. This is useful for synthesizing quantum circuits that implement arbitrary multi-qubit unitaries.
from pygridsynth.multi_qubit_unitary_approximation import (
approximate_multi_qubit_unitary,
)
from pygridsynth.mymath import random_su
# Generate a random SU(2^n) unitary
num_qubits = 2
U = random_su(num_qubits)
epsilon = "1e-10"
# Approximate with high precision
circuit, U_approx = approximate_multi_qubit_unitary(U, num_qubits, epsilon)
Returning DOmegaMatrix:
from pygridsynth.multi_qubit_unitary_approximation import (
approximate_multi_qubit_unitary,
)
from pygridsynth.mymath import random_su
# Generate a random SU(2^n) unitary
num_qubits = 2
U = random_su(num_qubits)
epsilon = "1e-10"
# Return DOmegaMatrix instead of mpmath.matrix for more efficient representation
circuit, U_domega = approximate_multi_qubit_unitary(
U, num_qubits, epsilon, return_domega_matrix=True
)
# Convert to complex matrix if needed
U_complex = U_domega.to_complex_matrix
Parameters:
U: Target unitary matrix (mpmath.matrix)
num_qubits: Number of qubits
epsilon: Error tolerance (can be str, float, or mpmath.mpf)
return_domega_matrix: If True, returns DOmegaMatrix; if False, returns mpmath.matrix (default: False)
scale_epsilon: Whether to scale epsilon based on the number of qubits (default: True)
cfg: Optional GridsynthConfig object for advanced configuration
**kwargs: Additional configuration options (ignored if cfg is provided)
Returns:
A tuple of (circuit, U_approx):
circuit: QuantumCircuit object representing the Clifford+T decomposition
U_approx: Approximated unitary matrix (mpmath.matrix or DOmegaMatrix depending on return_domega_matrix)
Mixed Unitary Synthesis
pygridsynth also provides functionality for mixed unitary synthesis, which approximates a target unitary by mixing multiple perturbed unitaries. This is useful for reducing the number of T-gates in quantum circuits.
The library provides two versions: mixed_synthesis_parallel (for parallel execution) and mixed_synthesis_sequential (for sequential execution).
Basic usage with mpmath.matrix:
from pygridsynth.mixed_synthesis import mixed_synthesis_sequential
from pygridsynth.mymath import diamond_norm_error_from_choi, random_su
# Generate a random SU(2^n) unitary matrix
num_qubits = 2
unitary = random_su(num_qubits)
# Parameters
eps = 1e-4 # Error tolerance
M = 64 # Number of Hermitian operators for perturbation
seed = 123 # Random seed for reproducibility
# Compute mixed synthesis (sequential version)
result = mixed_synthesis_sequential(unitary, num_qubits, eps, M, seed=seed)
if result is not None:
circuit_list, eu_np_list, probs_gptm, u_choi, u_choi_opt = result
print(f"Number of circuits: {len(circuit_list)}")
print(f"Mixing probabilities: {probs_gptm}")
error = diamond_norm_error_from_choi(u_choi, u_choi_opt, eps, mixed_synthesis=True)
print(f"error: {error}")
Using parallel version:
import mpmath
from pygridsynth.mixed_synthesis import mixed_synthesis_parallel
# Generate a random SU(2^n) unitary matrix
num_qubits = 2
unitary = mpmath.eye(2**num_qubits)
# Parameters
eps = 1e-4 # Error tolerance
M = 64 # Number of Hermitian operators for perturbation
seed = 123 # Random seed for reproducibility
# For faster computation with multiple cores
result = mixed_synthesis_parallel(unitary, num_qubits, eps, M, seed=seed)
Parameters:
unitary: Target unitary matrix (mpmath.matrix or numpy.ndarray)
num_qubits: Number of qubits
eps: Error tolerance parameter
M: Number of Hermitian operators for perturbation
seed: Random seed for reproducibility (default: 123)
dps: Decimal precision (default: -1 for auto-calculation)
Returns:
A tuple of (circuit_list, eu_np_list, probs_gptm, u_choi_opt) or None on failure:
circuit_list: List of QuantumCircuit objects for perturbed unitaries
eu_np_list: List of approximated unitary matrices (numpy arrays)
probs_gptm: Array of mixing probabilities
u_choi_opt: Optimal mixed Choi matrix
Note: The parallel version (mixed_synthesis_parallel) uses multiprocessing and may be faster for large M values, while the sequential version (mixed_synthesis_sequential) is more suitable for debugging or when parallel execution is not desired.
Contributing
Bug reports and feature requests are welcome. Please submit them via the GitHub repository Issues section. Contributions must comply with the MIT License.
Peter Selinger. "Efficient Clifford+T approximation of single-qubit operators." Quantum Info. Comput. 15, no. 1-2 (2015): 159-180.
Neil J. Ross and Peter Selinger. "Optimal ancilla-free Clifford+T approximation of z-rotations." Quantum Info. Comput. 16, no. 11-12 (2016): 901-953.
Vadym Kliuchnikov, Dmitri Maslov, and Michele Mosca. "Fast and efficient exact synthesis of single-qubit unitaries generated by Clifford and T gates." Quantum Info. Comput. 13, no. 7-8 (2013): 607-630.
Brett Gordon Giles and Peter Selinger. "Remarks on Matsumoto and Amano's normal form for single-qubit Clifford+T operators." ArXiv abs/1312.6584 (2013). URL: https://api.semanticscholar.org/CorpusID:10077777.
Vivek V. Shende, Igor L. Markov, and Stephen S. Bullock. "Minimal universal two-qubit controlled-NOT-based circuits." Phys. Rev. A 69, no. 6 (2004): 062321. DOI: 10.1103/PhysRevA.69.062321.
Vivek V. Shende, Igor L. Markov, and Stephen S. Bullock. "Finding Small Two-Qubit Circuits." Proceedings of SPIE - The International Society for Optical Engineering (2004). DOI: 10.1117/12.542381.
Vivek V. Shende, Igor L. Markov, and Stephen S. Bullock. "Smaller Two-Qubit Circuits for Quantum Communication and Computation." In Proceedings of the Conference on Design, Automation and Test in Europe - Volume 2, p. 20980. IEEE Computer Society, 2004.
Anna M. Krol and Zaid Al-Ars. "Beyond quantum Shannon decomposition: Circuit construction for n-qubit gates based on block-ZXZ decomposition." Physical Review Applied 22, no. 3 (2024): 034019. DOI: 10.1103/PhysRevApplied.22.034019.
Use together with pygridsynth: extensions, drivers, servers and companion tools.
qiskitPythie’s Index: 91An open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.1.1M downloads/mo