6
$\begingroup$

I'm writing a simple multiplication algorithm that uses the Quantum Fourier Transform to repetitively add a number (the multiplicand) to itself and decrements another number (the multiplier). The repeated addition process is to be stopped once the multiplier hits the fundamental state (all qubits are in the zero state). Registers a, b, c hold the product, multiplicand and multiplier respectively. Classical register cl is used to store the final result:

def multiply(first, second, n, m):  
    a = QuantumRegister(m+n, "a")
    b = QuantumRegister(m+n, "b")
    c = QuantumRegister(m, "c")
    d = QuantumRegister(m, "d")
    cl = ClassicalRegister(m+n, "cl")
    qc = QuantumCircuit(a, b, c, d, cl, name="qc")

    for i in range(0, n):
        if first[i] == "1":
            qc.x(b[n-(i+1)])
        if second[i] == "1":
            qc.x(c[m-(i+1)])
    qc.x(d[0])

    for i in range(0, m+n):
        createInputState(qc, a, m+n-(i+1))

    for i in range(m):
        createInputState(qc, c, m-(i+1))

At this point, however, I need to create a while loop of sorts that allows me to add the multiplicand to the accumulator (register a) until register c is in the fundamental state. Unfortunately the only method I could think of was using a for loop with range (0, (value of multiplier)), but I want to find out if there is a more 'quantum' alternative. The while loop would need to have work as below:

while (register c is not in the fundamental state):
        for i in range(0, m+n):
            evolveQFTState(qc, a, b, m+n-(i+1)) 
        for i in range(0, m):
            decrement(qc, c, d, m-(i+1))
        for i in range(0, m):
            inverseQFT(qc, c, i)

And then we wrap things up:

    for i in range(0, m+n):
        inverseQFT(qc, a, i)
    for i in range(0, m+n):
        qc.measure(a[i], cl[i])

So, in short, I am looking for a way to implement a set of statments that execute while a given condition holds true, i.e. a quantum register is not in the fundamental state. The problem I face is due to the fact that, to the best of my knowledge, we cannot use classical register bits in if statements, such as below:

if c[0] == 0:   -------> not possible for QISkit classical register bits
    #Do something 

Another approach I tried was to perform the decrement operation in a different quantum circuit, but I got error messages.

Note: This is my first question here on QC SE, so please let me know if I have to rephrase it, change it or provide any additional information.

$\endgroup$

1 Answer 1

6
$\begingroup$

Qiskit makes and manipulates quantum circuits specified by the OpenQASM standard. This does indeed support statements that are conditional on a classical register.

The if statement conditionally executes a quantum operation based on the value of a classical register. So you can have statements like

if(c==3) U(theta, phi, lambda) q[0];

This will perform the rotation U(theta, phi, lambda) on q[0] if the classical register c is holds the bit string that corresponds to the number 3.

As @sashwat-anagolum pointed out in a comment, at the the Qiskit level this can be done with

quantumCircuit.U(theta, lambda, phi).c_if(classicalRegister, value)

Note that classical conditionals like these are not currently supported on quantum hardware.

$\endgroup$
2
  • 1
    $\begingroup$ Thanks for the answer. In QISkit the above takes this form: quantumCircuit.U(theta, lambda, phi).c_if(classicalRegister, value) $\endgroup$ Commented Sep 24, 2018 at 13:00
  • $\begingroup$ Classical If , loops are used to dynamically construct register and cannot execute on device. c_if is for classicalRegister_if $\endgroup$
    – hungryMind
    Commented Feb 23, 2023 at 19:35

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.