2
$\begingroup$

Consider a circuit where you have 6 qubits and apply some gates. Then, you measure qubits 1 , 2 , 3 with results $c_1 , c_2 , c_3$. Next you apply $X^{c_1}$ on the 4-th qubit, $X^{c_1 + c_2}$ on the 5-th qubit and $X^{c_1 + c_2 + c_3}$ on the 6-th qubit. How can I write this circuit in Qiskit? I can't find a way to sum the results of the classical register.

$\endgroup$
2
  • $\begingroup$ By sum, do you mean "sum modulo 2"? $\endgroup$ Nov 20, 2023 at 8:57
  • $\begingroup$ @Egretta.Thula Since $X^2 = \mathbb{1}$, it should be the same thing. $\endgroup$
    – ar3q5dr9
    Nov 20, 2023 at 12:29

2 Answers 2

1
$\begingroup$

You can use bit_xor function from the newly added classical expressions module

from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.circuit.classical import expr

qr = QuantumRegister(6, 'q')
c1 = ClassicalRegister(1, 'c1')
c2 = ClassicalRegister(1, 'c2')
c3 = ClassicalRegister(1, 'c3')
out = ClassicalRegister(3, 'o')
circ = QuantumCircuit(qr, c1, c2, c3, out)

# Sample operations for demonstration purposes
circ.h([0, 1, 2])

circ.measure(qr[0], c1[0])
circ.measure(qr[1], c2[0])
circ.measure(qr[2], c3[0])

with circ.if_test((c1, 1)):
    circ.x(qr[3])

with circ.if_test(expr.equal(expr.bit_xor(c1, c2), 1)):
    circ.x(qr[4])

with circ.if_test(expr.equal(expr.bit_xor(expr.bit_xor(c1, c2), c3), 1)):
    circ.x(qr[5])

circ.barrier()
circ.measure(qr[3:6], out)

circ.draw('mpl', style='textbook')

The circuit should look like:

enter image description here

$\endgroup$
1
$\begingroup$

This can be done quite easily. For instance, take the case where you have two classical bits $c_1, c_2$ and would like to implement $X^{c_1+c_2}$ on a qubit $q$. Clearly, applying $X^{c_1}$ followed by $X^{c_2}$ on $q$ applies $X$ on $q$ only if exactly one of $c_1$ and $c_2$ equals $1$. If both $c_1$ and $c_2$ are $0$ then $X$ is never applied, and if both bits are $1$, then $X$ is applied twice on $q$, which is $X^2 = I$.

Similarly, if you have three classical bits and would like to apply $X^{c_1+c_2+c_3}$ on $q$, then applying $X^{c_1}, X^{c_2}$ and $X^{c_3}$ serially, gives you the desired result of $X^{c_1+c_2+c_3}$ on $q$. As a check, see that if exactly one of $c_1, c_2, c_3$ equals $1$, then $X$ is implemented, and if exactly two of the bits are $1$, then $X$ is applied twice on $q$, which is $X^2=I$. Finally, if all three bits are $1$, then $X$ is applied thrice on $q$, which is $X^3 = X$ as required.

Note that this will not just hold for $X$ gate but for any unitary gate $U$.

$\endgroup$
1
  • $\begingroup$ Thank you for the answer, but doesn't this procedure increase the depth of the circuit? For example, in a circuit of $2n$ qubits, if I measure the first $n$ and always get a $1$, then i need to apply $n$ $X$ gates on the last qubit of the circuit. If it is possibile to sum the results then i would need to apply only $0$ or $1$ $X$ gate. $\endgroup$
    – ar3q5dr9
    Nov 20, 2023 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.