Multiplication on Quantum Computers with Qiskit

Introduction

Interested in learning how to program quantum computers? Then check out our Qiskit textbook Introduction to Quantum Computing with Qiskit.

In this tutorial we will explore how to do multiplication using the QFT Multiplier circuit on IBM quantum computers in Qiskit.

On classical computers multiplication is done using a digital circuit called a binary multiplier. On quantum computers though it can be implemented through a variety of different quantum circuits.

The multiplication circuit we will be using in the tutorial is known as the QFT multiplier circuit. This was developed by Lidia Ruiz-Perez and Juan Garcia-Escartin who made the circuit available in their paper Quantum arithmetic with the Quantum Fourier Transform which you can find here: https://arxiv.org/pdf/1411.5949.pdf

The circuit works by applying the Quantum Fourier Transform (QFT) and then a series of QFT additions. The end state will correspond to the product of the two n-bit numbers.

Circuit diagram of the QFT multiplier circuit from the original paper: https://arxiv.org/pdf/1411.5949.pdf

For more information of the how QFT works you can look at our tutorial on it here: https://quantumcomputinguk.org/tutorials/quantum-fourier-transform-in-qiskit

Implementation

In Qiskit the QFT Multiplier circuit can be implemented very easily. For our example we will create a circuit that will multiply 2*3 and go through it step by step.

Step 1: Initialize the registers and quantum circuit

The first step is to initialise the registers and quantum circuit. Our circuit will consist of two registers. A quantum register that holds our qubits and a classical register that holds the bits used to measure the output qubits.

For our quantum register we will have 8 qubits:

  • Qubits 0 and 1 hold Operand A which we will use to store 2.

  • Qubits 2 and 3 hold Operand B which we will use to store 3.

  • Qubits 4 to 7 hold the output qubits that will hold the result.

The classical register will hold the result when we measure the output qubits and as such will have 4 bits.

In Qiskit the registers can circuit can be initialized using the following code:

q = QuantumRegister(8,'q')
c = ClassicalRegister(4,'c')

circuit = QuantumCircuit(q,c)

Step 2: Encode the values on to the Operands

The next step is to encode 2 on to Operand A and 3 on to Operand B.

Since 2 is ‘10’ in binary we leave qubit 0 as is so that its state is |0〉and apply a Pauli-X gate to qubit 1 so that it state flips from |0〉 to |1〉.

To encode 3 on Operand B we apply Pauli-X gates to qubits 2 and 3 since 3 is ‘11’ in binary.

The encodings are done with the following code:

# Operand A = 10 (2)
circuit.x(q[1])

# Operand B = 11 (3)
circuit.x(q[2])
circuit.x(q[3])

Step 3: Create the QFT multiplication circuit

The next step is to create the QFT multiplication circuit. Thankfully this is incredibly simple as the circuit has a dedicated class in Qiskit and as such can be added using 2 lines of code:

circuit1 = RGQFTMultiplier(num_state_qubits=2, num_result_qubits=4)
circuit = circuit.compose(circuit1)

The first line creates the multiplier circuit using the RGQFTMultiplier() function which we pass the following arguments in to:

num_state_qubits: The number of bits for each operand. Since we are doing 2*3 we only need 2 bits for each

num_result_qubits: This denotes the number of output qubits. For this we set it to 4 to prevent integer overflow.

The second line simply add the multiplier circuit (which we have denoted as circuit1) to our main circuit using the circuit.compose() method.

Step 4: Measure the output qubits

The next part is to measure the output qubits using the following code:

circuit.measure(q[4],c[0])
circuit.measure(q[5],c[1])
circuit.measure(q[6],c[2])
circuit.measure(q[7],c[3])

Step 5: Send the circuit to a backend device and get the results back

The final step is to send the circuit off to be run on a backend device and get the results and print them.

Note: Since this is only a tutorial we will be using a backend quantum simulator since it is much quicker. However you can set the backend device to be any real quantum device you have access to.

job = execute(circuit, backend, shots=2000)
result = job.result()
counts = result.get_counts()
print('2*3')
print('---')
print(counts)

How to run the program

Copy and paste the code below in to a python file

  1. Enter your API token in the IBMQ.enable_account('Insert API token here') part

  2. Save and run

Code

from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit, execute,IBMQ
from qiskit.tools.monitor import job_monitor
from qiskit.circuit.library import RGQFTMultiplier

IBMQ.enable_account('ENTER API KEY HERE')
provider = IBMQ.get_provider(hub='ibm-q')

backend = provider.get_backend('ibmq_qasm_simulator')

q = QuantumRegister(8,'q')
c = ClassicalRegister(4,'c')

circuit = QuantumCircuit(q,c)

# Operand A = 10 (2)
circuit.x(q[1])

# Operand B = 11 (3)
circuit.x(q[2])
circuit.x(q[3])

circuit1 = RGQFTMultiplier(num_state_qubits=2, num_result_qubits=4)
circuit = circuit.compose(circuit1)

circuit.measure(q[4],c[0])
circuit.measure(q[5],c[1])
circuit.measure(q[6],c[2])
circuit.measure(q[7],c[3])

print(circuit)

job = execute(circuit, backend, shots=2000)
result = job.result()
counts = result.get_counts()

print('2*3')
print('---')
print(counts)

Output

Output when performing 2*3. Note the result 0110 = 6