Skip to main content

trasyn: a unitary synthesis method for fault-tolerant quantum computing

Project description

trasyn: tensor-based arbitrary unitary synthesis

trasyn is a gate synthesis algorithm for early fault-tolerant quantum computing. trasyn natively synthesizes arbitrary single-qubit unitaries to reduce the resulting non-Clifford gate count. For details, check out our paper Reducing T Gates with Unitary Synthesis. For the original code and data for the paper, please refer to the paper branch.

Install

Minimum Installation

pip install trasyn

GPU Acceleration

It is highly recommended to install cupy for GPU acceleration, which significantly speeds up the computation:

pip install trasyn[cupy-cuda12] # or [cupy-cuda11] depending on CUDA version

The CUDA Toolkit version can be found using nvcc --version.

Qiskit Integration

Optionally, qiskit may be installed to support features like full-circuit transpilation and synthesis.

pip install trasyn[qiskit]

Usage

Synthesize a single-qubit unitary

trasyn.synthesize(target, nonclifford_budget) takes a target unitary matrix (in the form of a numpy array, three angles for U(theta, phi, lam), or a single Rz angle) and a non-Clifford gate count budget and outputs a gate sequence string of the target gate set, the corresponding matrix, and the distance to the target.

>>> seq, mat, err = trasyn.synthesize(trasyn.gates.t(), nonclifford_budget=10)
>>> print(seq, err)
t 0.0

>>> seq, mat, err = trasyn.synthesize([0.1, 0.2, 0.3], nonclifford_budget=20) # U(0.1, 0.2, 0.3)
>>> print(seq, err, seq.count("t"))
yththyththththxthththythththxththxththxththxthsz 0.0018002056473114445 19

>>> seq, mat, err = trasyn.synthesize(pi / 16, 30, error_threshold=0.001) # Rz(pi/16)
>>> print(seq, err, seq.count("t"))
hththththxththththththxthththxththththththxththths 0.0005551347294707683 22

>>> seq, mat, err = trasyn.synthesize(pi / 16, 30, logical_error_rates={"t": 1e-5}) # execution-error-aware synthesis
>>> print(seq, err, seq.count("t"))
hththxthythththythththththythththxthythth 0.006293156923603058 17

Full list of synthesize() arguments

  • target_unitary : NDArray | Sequence[float] | float

    The target unitary matrix, three angles for U(theta, phi, lam), or a single Rz angle.

  • nonclifford_budget : int | Sequence[int]

    The budget for non-Clifford gates (e.g. T gates). Can be a single integer representing the total budget or a sequence of integers specifying the budget for each tensor.

  • error_threshold : float, optional

    The synthesis error threshold for the method to return once it is met. Note that this is not a hard constraint; the method will return the best solution found after all attempts if the error threshold is not met or it is not specified. Default is None.

  • gate_set : "tsh" or "tshxyz", optional

    The target gate set. Gates are listed in the order of cost. Additional gate sets can be added with the unique_matrices.py script. This process will be made more user-friendly in the future. Default is "tshxyz".

  • num_attempts : int, optional

    The number of sampling attempts per budget configuration. Default is 5.

  • num_samples : int, optional

    The number of samples to in the sampling process. If None, it is calculated based on available memory. Default is None.

  • logical_error_rates : dict[str, float], optional

    A dictionary mapping gate names to their logical error rates. If provided, the synthesis will minimize the overall process infidelity, which consists of the synthesis error and the execution error for performing the gates. Default is None.

  • gpu : bool, optional

    Whether to use GPU for synthesis. Default is True.

  • rng : numpy.random.Generator | int, optional

    Random number generator or seed for reproducibility. Default is None.

  • verbose : bool, optional

    Whether to print verbose output during synthesis. Default is False.

Synthesize a Qiskit circuit

Given a Qiskit circuit, trasyn.synthesize_qiskit_circuit(circuit, u3_transpile=True) transpiles it to CNOT+U3 and synthesizes each U3 gate to bring the full circuit to Clifford+T. The transpilation step attempts various configurations to minimize the nontrivial U3 count and thus, the total T count.

Use in Command-line

Synthesize a single-qubit unitary:

$ trasyn 'rz(pi/16)' 30
Sequence: hththththxththththththxthththxththththththxththths, Error: 0.0005551347294707683, T-count: 22

Synthesize a qasm circuit (Qiskit needs to be installed):

$ trasyn circuit.qasm 20 -s synthesized.qasm

Full list of flags

usage: trasyn [-h] [-e ERROR_THRESHOLD] [-g GATE_SET] [--num-attempts NUM_ATTEMPTS] [--num-samples NUM_SAMPLES] [-c] [--seed SEED] [-v] [-s SAVE_PATH] [--skip-transpile] target budget

Synthesize a single-qubit unitary or a qasm circuit to Clifford+T.

positional arguments:
  target                An expression of the target unitary or a filename, e.g. 'Rz(3*pi/8)', 'u(0.1, pi+0.2, 0.3**0.5)', 'unitary.npy', or 'circuit.qasm'.
  budget                The non-Clifford gate budget for synthesis.

options:
  -h, --help            show this help message and exit
  -e ERROR_THRESHOLD, --error-threshold ERROR_THRESHOLD
                        The synthesis error threshold for the method to return once it is met. Note that this is not a hard constraint; the method will return the best solution found after all attempts if the error threshold is not
                        met or it is not specified. Default is None.
  -g GATE_SET, --gate-set GATE_SET
                        The target gate set. Gates are listed in the order of cost. Additional gate sets can be added with the unique_matrices.py script. This process will be made more user-friendly in the future. Default is
                        'tshxyz'.
  --num-attempts NUM_ATTEMPTS, --na NUM_ATTEMPTS
                        The number of sampling attempts per budget configuration. Default is 5.
  --num-samples NUM_SAMPLES, --ns NUM_SAMPLES
                        The number of samples in the sampling process. If None, it is calculated based on available memory. Default is None.
  -c, --cpu-only        Do not use GPU for synthesis.
  --seed SEED           Random seed for reproducibility.
  -v, --verbose         Enable verbose output.
  -s SAVE_PATH, --save-path SAVE_PATH
                        Path to save the synthesized circuit. Default is '<target>_synthesized.qasm' if target is a QASM circuit.
  --skip-transpile, --st
                        Skip the transpilation step for circuit synthesis.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trasyn-0.3.0.tar.gz (18.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trasyn-0.3.0-py3-none-any.whl (18.1 MB view details)

Uploaded Python 3

File details

Details for the file trasyn-0.3.0.tar.gz.

File metadata

  • Download URL: trasyn-0.3.0.tar.gz
  • Upload date:
  • Size: 18.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for trasyn-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cff8513bbb338ee07262ffa3906aba05e1c3dceeb4b72bbfaaddb61ce34ee0eb
MD5 11b44394d3d95c4e6faa62e4d5f6426b
BLAKE2b-256 5fd762ba50d6c8ad52073b5ec284913830a4ad17e807ca78cb80afb8670f6088

See more details on using hashes here.

Provenance

The following attestation bundles were made for trasyn-0.3.0.tar.gz:

Publisher: python-publish.yml on HaoTy/trasyn

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trasyn-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: trasyn-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 18.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for trasyn-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4caa911e2e9b115e3e823a738095b8e69c110f764e49ab24371a3e3753b401d7
MD5 d31a85b5816b86c8ea67903e06561cdf
BLAKE2b-256 49f435420d45bf1585e8c256ad6ebd274b5b6c03e4d537f7b880c72a38b48515

See more details on using hashes here.

Provenance

The following attestation bundles were made for trasyn-0.3.0-py3-none-any.whl:

Publisher: python-publish.yml on HaoTy/trasyn

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page