Skip to content

Computation Basis State Preparation

The prepare_int and inplace_prepare_int functions create a single state computational basis state, indexed by the given integer argument.

The prepare_int will allocate a QArray with the minimal required size for representing the given integer argument. inplace_prepare_int on the other hand will prepare the state on a given QArray. It is more suitable were on wants to prepare a state with a given size.

Function: prepare_int

Arguments:

  • value: CInt - The index of the state to prepare in the computational basis.
  • target: Output[QArray[QBit]]

Function: inplace_prepare_int

Arguments:

  • value: CInt - The index of the state to prepare in the computational basis.
  • target: QArray[QBit] - The quantum array to prepare the state at. Should be of size at least \(\lceil \log_2{value}\rceil\).

Example 1

Prepare the \(|3\rangle\) state (will result in a 2 qubits state space):

from classiq import Output, QArray, QBit, create_model, prepare_int, qfunc


@qfunc
def main(x: Output[QArray[QBit]]):
    prepare_int(3, x)


qmod = create_model(main)
from classiq import synthesize, write_qmod

write_qmod(qmod, "prepare_int")
qprog = synthesize(qmod)

Example 2

Prepare the \(|3\rangle\) state, in a state space of 5 qubits:

from classiq import (
    Output,
    QArray,
    QBit,
    allocate,
    create_model,
    inplace_prepare_int,
    qfunc,
)


@qfunc
def main(x: Output[QArray[QBit]]):
    allocate(5, x)
    inplace_prepare_int(3, x)


qmod = create_model(main)
from classiq import synthesize, write_qmod

write_qmod(qmod, "inplace_prepare_int")
qprog = synthesize(qmod)