Blog
18
June
,
2025

Concatenation Operator

Share the article
Our library

The concatenation operator is a new Qmod language feature that helps us operate on a quantum object created ad-hoc from parts of other objects. For example, the following Qmod model calls hadamard_transform with the concatenation [x1[3], x2[1:3], x1[0]].

@qfunc
def main():
    x1 = QArray(length=4)
    allocate(x1)
    x2 = QArray(length=4)
    allocate(x2)
    hadamard_transform([x1[3], x2[1:3], x1[0]])

A concatenation specifies a sequence of (possibly partial) quantum variables. In Python, this is done with a Python list (the Native Qmod syntax is {...}). In the model above, function hadamard_transform is passed a concatenation of three variable parts: x1[3], x2[1:3], and x1[0]. The qubits belonging to these parts are packed into a new array variable right before they are sent to hadamrd_transform. After compiling the model, we can watch the resulting circuit on the Classiq IDE. We see that hadamard_transform is applied to the qubits specified in the concatenation, namely, the first and last qubits of x1 and the middle two qubits of x2.

The concatenation operator is not only a convenient feature, but it also helps the programmer to avoid quantum programming bugs. For instance, consider the following model:

@qfunc
def my_control_h(ctrl: QBit, target: QArray):
    control(ctrl, lambda: hadamard_transform(target))


@qfunc
def main():
    x1 = QArray(length=4)
    allocate(x1)
    x2 = QArray(length=4)
    allocate(x2)
    my_control_h(x2[2], [x1[3], x2[1:3], x1[0]])

Function my_control_h receives a qubit ctrl and an array target and applies a controlled Hadamard gate to ctrl and each qubit of target. The main function calls my_control_h with x2[2] as the control and a concatenation [x1[3], x2[1:3], x1[0]] as the target. When we compile the program, we get the following error:

Quantum cloning violation: Arguments 'x2[2]' and 'x2[1:3]' overlap

(This error points to the my_control_h call.)

Using the same qubit in two different arguments of a quantum gate is not feasible in quantum computation. In the my_control_h call, the qubit of the first argument x2[2] also appears in the concatenation, as the second qubit of x2[1:3]. This has to be a mistake! The Qmod compiler detects this violation and reports an error.

Currently, concatenations are only supported in function call arguments.

The concatenation operator is a new Qmod language feature that helps us operate on a quantum object created ad-hoc from parts of other objects. For example, the following Qmod model calls hadamard_transform with the concatenation [x1[3], x2[1:3], x1[0]].

@qfunc
def main():
    x1 = QArray(length=4)
    allocate(x1)
    x2 = QArray(length=4)
    allocate(x2)
    hadamard_transform([x1[3], x2[1:3], x1[0]])

A concatenation specifies a sequence of (possibly partial) quantum variables. In Python, this is done with a Python list (the Native Qmod syntax is {...}). In the model above, function hadamard_transform is passed a concatenation of three variable parts: x1[3], x2[1:3], and x1[0]. The qubits belonging to these parts are packed into a new array variable right before they are sent to hadamrd_transform. After compiling the model, we can watch the resulting circuit on the Classiq IDE. We see that hadamard_transform is applied to the qubits specified in the concatenation, namely, the first and last qubits of x1 and the middle two qubits of x2.

The concatenation operator is not only a convenient feature, but it also helps the programmer to avoid quantum programming bugs. For instance, consider the following model:

@qfunc
def my_control_h(ctrl: QBit, target: QArray):
    control(ctrl, lambda: hadamard_transform(target))


@qfunc
def main():
    x1 = QArray(length=4)
    allocate(x1)
    x2 = QArray(length=4)
    allocate(x2)
    my_control_h(x2[2], [x1[3], x2[1:3], x1[0]])

Function my_control_h receives a qubit ctrl and an array target and applies a controlled Hadamard gate to ctrl and each qubit of target. The main function calls my_control_h with x2[2] as the control and a concatenation [x1[3], x2[1:3], x1[0]] as the target. When we compile the program, we get the following error:

Quantum cloning violation: Arguments 'x2[2]' and 'x2[1:3]' overlap

(This error points to the my_control_h call.)

Using the same qubit in two different arguments of a quantum gate is not feasible in quantum computation. In the my_control_h call, the qubit of the first argument x2[2] also appears in the concatenation, as the second qubit of x2[1:3]. This has to be a mistake! The Qmod compiler detects this violation and reports an error.

Currently, concatenations are only supported in function call arguments.

About "The Qubit Guy's Podcast"

Hosted by The Qubit Guy (Yuval Boger, our Chief Marketing Officer), the podcast hosts thought leaders in quantum computing to discuss business and technical questions that impact the quantum computing ecosystem. Our guests provide interesting insights about quantum computer software and algorithm, quantum computer hardware, key applications for quantum computing, market studies of the quantum industry and more.

If you would like to suggest a guest for the podcast, please contact us.

See Also

No items found.

Start Creating Quantum Software Without Limits

contact us