Register Indexing and Slicing¶
The Classiq platform allows referring to specific qubits or qubit ranges of quantum registers such as lists and tuples, in a similar manner to Python sequences. The indexing is zero based.
To obtain multiple consecutive indices, commonly referred to as slicing:
- Specify the start (inclusive) and stop (exclusive) indices.
- Use a colon separator.
- Slice with a step size different from
1
with two colon separators.
Examples:
left_arg[5]
refers to qubit5
of registerleft_arg
. Note: qubit5
is the 6th qubit due to zero-based indexing.left_arg[2:5]
refers to qubits2
,3
,4
(the slicing stop is exclusive)left_arg[2:8:2]
refers to qubits2
,4
,6
(again, the slicing stop is exclusive)left_arg[:-1]
refers to all qubits except the last one
The example below generates an iQFT-CX-QFT circuit, where the value of the CX control bit is obtained from the most significant bit of the iQFT. This block is used in the modular addition circuit, which is the building block of Beauregard's Shor algorithm implementation [1].
In the Python SDK, the slicing is on the QReg
object that connects to the input/output
of the function.
Additionally, the Python SDK allows concatenation and in-place assignment of QReg
objects,
as seen in the example.
{
"function_library": {
"functions": [
{
"name": "main",
"logic_flow": [
{
"function": "QFT",
"name": "iqft",
"function_params": {"num_qubits": 6},
"is_inverse": true,
"outputs": {"IN[:-1]": "lsb", "IN[-1]": "msb_inv"}
},
{
"function": "CXGate",
"name": "cx",
"function_params": {},
"inputs": {"CTRL": "msb_inv"},
"outputs": {"CTRL": "msb"}
},
{
"function": "QFT",
"name": "qft",
"function_params": {"num_qubits": 6},
"inputs": {"IN[:-1]": "lsb", "IN[-1]": "msb"}
}
]
}
]
}
}
from classiq.builtin_functions import QFT, CXGate
from classiq import Model, QReg
model = Model()
qft_params = QFT(num_qubits=6)
cx_params = CXGate()
qreg = model.QFT(qft_params, is_inverse=True, call_name="iqft")["IN"]
msb = model.CXGate(
cx_params,
call_name="cx",
in_wires={"CTRL": qreg[-1]},
)["CTRL"]
# Alternatively, use the following syntax:
# qreg[-1] = msb
qreg = QReg.concat(qreg[:-1], msb)
model.QFT(
qft_params,
call_name="qft",
in_wires={"IN": qreg},
)
circuit = model.synthesize()
circuit.show()
The output circuit is shown below at the functional level.
[1] Stephane Beauregard. (2003). Circuit for Shor's algorithm using 2n+3 qubits. Quantum Info. Comput. 3, 2, 175–185.