Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
This tutorial demonstrates automatic arithmetic operation management by the synthesis engine. It synthesizes a complex arithmetic expression, where uncomputation procedure, together with initialization and reuse of auxiliary qubits, are all automated. Given different global width or depth constraints results in different circuits. Define a quantum model that applies some quantum arithmetic operation on QNum variables.
from classiq import *
from classiq.qmod.symbolic import max


@qfunc
def main(x: Output[QNum], y: Output[QNum], z: Output[QNum]):
    allocate(2, x)
    allocate(1, y)
    hadamard_transform(x)
    hadamard_transform(y)
    z |= (2 * x + y + max(3 * y, 2)) > 4

You can try different optimization scenarios, below we introduce two examples:

  1. Optimizing over depth and constraining the maximal width to 10 qubits.
  2. Optimizing over depth and constraining the maximal width to 12 qubits.
Optimizing over depth and constraining the maximal width to 10 qubits
NUM_QUBITS_1 = 10
qprog_1 = synthesize(
    main,
    constraints=Constraints(optimization_parameter="depth", max_width=NUM_QUBITS_1),
)
show(qprog_1)
sample(qprog_1)
Output:

Quantum program link: https://platform.classiq.io/circuit/3DcBR5bi1Kg3ZdQpUAakewAKcSh
  

Output:

Submitting job to simulator
  Job: https://platform.classiq.io/jobs/6c500169-a27b-4b80-863f-be1996c1c08c
  

xyzcountsprobabilitybitstring
03012750.1342771011
11112740.1337891101
23112610.1274411111
30102550.1245120100
42012530.1235351010
52112520.1230471110
60002420.1181640000
71002360.1152340001
Change the quantum model constraint to treat the second scenario for optimizing over depth and constraining the maximal width to 12 qubits:
NUM_QUBITS_2 = 12
qprog_2 = synthesize(
    main,
    constraints=Constraints(optimization_parameter="depth", max_width=NUM_QUBITS_2),
)
show(qprog_2)
sample(qprog_2)
Output:

Quantum program link: https://platform.classiq.io/circuit/3DcBe5FXGO6GThlRywJnBjDrEma
  

Output:

Submitting job to simulator
  Job: https://platform.classiq.io/jobs/7eafa5de-83a2-460a-ace2-e2e7332600a6
  

xyzcountsprobabilitybitstring
02012710.1323241010
10102660.1298830100
20002600.1269530000
33112580.1259771111
41002540.1240230001
51112540.1240231101
62112450.1196291110
73012400.1171881011