Skip to content

Auxiliary Reuse and Management

This tutorial demonstrates automatic auxiliary qubits management by the synthesis engine. It synthesizes a simple state preparation function, which comprises several multi-controlled rotations. These rotations can use auxiliary qubits to reduce depth. For a given global width constraint, the initialization and reuse of auxiliary qubits between different function blocks is automated.

import numpy as np

from classiq import (
    Constraints,
    CustomHardwareSettings,
    OptimizationParameter,
    Preferences,
    QArray,
    QuantumProgram,
    create_model,
    prepare_amplitudes,
    qfunc,
    set_constraints,
    set_preferences,
    show,
    synthesize,
)

NUM_QUBITS = 4
np.random.seed(12)
amplitudes = 1 - 2 * np.random.rand(2**NUM_QUBITS)
amplitudes = (amplitudes / np.linalg.norm(amplitudes)).tolist()


preferences = Preferences(
    custom_hardware_settings=CustomHardwareSettings(basis_gates=["cx", "u"]),
    random_seed=1235,
    optimization_timeout_seconds=100,
)


@qfunc
def main() -> None:
    out = QArray("out")
    prepare_amplitudes(amplitudes=amplitudes, bound=0.2, out=out)


qmod = create_model(main)
qmod = set_preferences(qmod, preferences)
constraints = Constraints(
    optimization_parameter=OptimizationParameter.DEPTH, max_width=8
)
qmod = set_constraints(qmod, constraints)
qprog = synthesize(qmod)

circuit = QuantumProgram.from_qprog(qprog)


show(qprog)
Opening: https://platform.classiq.io/circuit/696c857c-20ab-4dad-acb9-3f7e98875450?version=0.41.0.dev39%2B79c8fd0855