John Boyer
Dec 3, 2018 · 8 min read

Introduction

The purpose of this notebook article is to demonstrate using quantum operations to achieve computing objectives. The term quantum operation refers to a higher level method that may be implemented with one or many low-level quantum gates. More generally, the goal is to demonstrate the notion that quantum algorithms use quantum operations to implement constraints that coerce qubits from representing any possible outcomes to representing the outcomes that satisfy the constraints.

Using John Preskill’s terminology, we now have “noisy intermediate scale quantum” (NISQ) computers that can obtain a desired outcome with high probablility, i.e. where the desired outcome rises well above the noise that can occur within the current early-stage quantum computing devices. In this notebook article, we will implement a classical computing algorithm in order to see how differently it is done in quantum computing, and we will see that the desired outcome occurs by far the most frequently. However, note that the emphasis is on understanding how the quantum operations achieve an easily understood result, and so there is no quantum computing speedup in this case.

In this noteboook article, we will create a quantum circuit that uses quantum operations to perform addition of two single bit numbers. This problem reduces to developing quantum operation sequences that perform a classical ‘XOR’ operation to calculate the least significant bit and a classical ‘AND’ operation to calculate the most significant bit of the answer. This can be seen in the two columns of the expected answers below:

0+0=00
0+1=01
1+0=01
1+1=10

Qiskit Installation and Import

The Quantum Information Science development kit, or Qiskit, is a library and framework for either connecting to and running quantum computing programs on a real IBM Q quantum computer or simulating them on the user’s classical computing environment. The first cell below contains code to run once to get Qiskit installed. The second cell below should be run any time the notebook starts to import the parts of Qiskit relevant to this notebook’s operations.

[1] !pip install --user qiskit
[2] from qiskit import QuantumCircuit, ClassicalRegister
from qiskit import QuantumRegister
from qiskit import execute, IBMQ, Aer, QISKitError
from qiskit.backends.ibmq import least_busy

The Memory Model for the Quantum Circuit

This notebook uses qubits q0 and q1 for the inputs. Qubit q2 will be used for the least significant bit of the answer, and qubit q3 will be for the most significant bit.

[3] num_qubits = 4
q = QuantumRegister(num_qubits)
c = ClassicalRegister(num_qubits)

Quantum Circuit Initialization and Input

The following cell creates the quantum circuit with the quantum and classical registers. Then, it assigns the input to qubits q0 and q1.

The ground state |0⟩ is the default, so an X gate is used on qubits that must start in the excited state |1⟩. The X gate performs a pi radian rotation about the X-axis, which rotates |0⟩ (a.k.a. |+z⟩) through the Y-axis to |1⟩ (a.k.a. |-z⟩). The X gate is sometimes called a NOT gate, but note that it performs a pi radian rotation that happens to perform a classical NOT, or bit flip, only when the qubit is in |0⟩ or |1⟩ state. To change the input, comment out the X gate operation on any qubits that should be |0⟩ and ensure the X gate is not commented on any qubits that should be initialized to |1⟩.

[4] qc = QuantumCircuit(q, c)
qc.x(q[0])
qc.x(q[1])

Performing ‘XOR’ with Quantum Operations

An ‘XOR’ can be performed with two quantum operations. The inputs of the ‘XOR’ come from qubits q0 and q1, and the output of the ‘XOR’ will go to qubit q2. The output qubit, q2, starts in the ground state, |0⟩.

We first apply a controlled-not operation with q2 as the target of the control and with q0 as the source. The controlled-not is also called CNOT, or CX. This operation negates the target if the source is excited (|1⟩). By itself, this operation changes q2 from |0⟩ to |1⟩ if q0 is |1⟩, and it leaves q2 unchanged if q0 is |0⟩.

Next, we apply a CNOT with qubit q2 as the target and with q1 as the source. If q1 is |0⟩, then q2 is unchanged from the effect of the CNOT with q0. Therefore, we have:

q0=|0⟩, q1=|0⟩ results in q2=|0⟩

q0=|1⟩, q1=|0⟩ results in q2=|1⟩

However, if q1 is |1⟩, then q2 is inverted relative to the effect of the CNOT with q0. Therefore, we have:

q0=|0⟩, q1=|1⟩ results in q2=|1⟩

q0=|1⟩, q1=|1⟩ results in q2=|0⟩

This concludes the method for performing ‘XOR’ with quantum operation, which calculates the least significant bit of the single bit addition result.

[5] qc.cx(q[0], q[2])
qc.cx(q[1], q[2])

Performing ‘AND’ with Quantum Operations

An ‘AND’ can be performed with three quantum operations. The inputs of the ‘AND’ come from qubits q0 and q1, and the output of the ‘AND’ will go to qubit q3. The output qubit, q3, starts in the ground state, |0⟩.

Operation 1. We target qubit q3 with a controlled-Hadamard operation that is controlled by the source qubit q0. This changes the target q3 from |0⟩ to |+x⟩ if the source q0 is |1⟩. The operation looks like this on the Bloch sphere:

Operation 2. Next, we target qubit q3 with a controlled-Z operation that is controlled by the source qubit q1. This changes the phase of the target q3 by rotating pi radians around Z-axis if the source qubit q1 is |1⟩. The operation looks like this on the Bloch sphere:

The following are the results so far:

  • For input q1 q0 = |0⟩ |0⟩, q3 is not changed from |0⟩
  • For input q1 q0 = |0⟩ |1⟩, q3 only changed to |+x⟩
  • For input q1 q0 = |1⟩ |0⟩, q3 is at |0⟩ because q0 did not rotate it, and q1 requests a Z-axis phase rotation, but |0⟩ is along the Z-axis, so rotating it does nothing.
  • For input q1 q0 = |1⟩ |1⟩, q3 is |-x⟩ due to pi phase rotation from |+x⟩

Operation 3. Finally, we target q3 with a controlled-Hadamard operation that is controlled by the source qubit q0. Note above that when input q0 is |0⟩, q3 is already in the correct state of |0⟩. Therefore, we only take a further action if q0 is |1⟩.

When q0 is |1⟩, then the controlled-Hadamard operation maps the X-axis to the Z-axis, so |+x⟩ is converted to |+z⟩=|0⟩ and |-x⟩ is converted to |-z⟩=|1⟩. The operation looks like this on the Bloch sphere:

This concludes the method for performing ‘AND’ with quantum operation, which calculates the most significant bit of the single bit addition result.

[6] qc.ch(q[0], q[3])
qc.cz(q[1], q[3])
qc.ch(q[0], q[3])

Perform the Measurement

Use this code to measure the state of the qubits, giving a classical computing answer.

[7] qc.measure(q, c)

Simulate the Quantum Circuit

On a simulator, use this code to execute the quantum circuit that defines the input, performs the processing, and measures the output. Then, render the output in the notebook user interface.

[8] simulator = Aer.get_backend('qasm_simulator')
job = execute(qc, simulator)
result = job.result()
print("Data Result:", result.get_data(qc))

Based on the initialization in cell [5] above, the simulator always produces the result ‘10’ (2) in qubits q3 and q2.

Run the Experiment on a Real IBM Q Quantum Computer

Now we will set up to run on a real IBM Q quantum computer. The first cell below contains code that only has to run once per Python run-time to get it to work with your IBM Q Experience account. The second cell should be run once per notebook session to load the user’s IBM Q quantum computer access token.

[9] IBMQ.save_account('your API token')
[10] IBMQ.load_accounts()

Now this cell obtains access to a real IBM Q quantum computer on which to compile and execute the code.

[11] result_counts = {}
try:
device = IBMQ.get_backend('ibmqx4')
job_exp = execute(qc, device, shots=1024, max_credits=10)
result_exp = job_exp.result()
result_counts = result_exp.get_counts(qc)
except:
print("Device is currently unavailable.")

In the results, the qubits are in the order q3, q2, q1, and q0, so we tally the outcomes based on the first two qubits as they are the output qubits.

[12] result_freqs = {'00':0, '01':0, '10':0, '11':0 }
for key in result_counts.keys():
freq_key = key[0:2]
result_freqs[freq_key] = result_freqs[freq_key] +
result_counts[key]
import matplotlib.pyplot as plt
D = result_freqs
plt.bar(range(len(D)), list(D.values()), align='center')
plt.xticks(range(len(D)), list(D.keys()))
plt.show()

There are four possible outcomes for the two output qubits: 00, 01, 10, and 11. The expected outcome is 10. With 1024 shots, the noise outcome would be on the order of 256 shots per possible outcome. In the frequency results shown below, one can see that, by far, the correct final state of the output qubits occurs more frequently than all other possible outcomes combined. A most frequently occurring quantum computing outcome is precisely what a quantum experimentalist would investigate first within their real world application.

Conclusion

In this notebook, we have demonstrated how quantum algorithms use quantum operations to coerce qubits into representing the outcome or outcomes that satisfy the constraints of a problem. In the case of quantum addition of two qubits initialized with classical bit values, one output qubit had to satisfy the constraint of being excited if and only if the two input qubits differed, and a second output qubit had to satisfy the constraint of being excited if and only if both input qubits were excited. Not only did we simulate this quantum circuit, we ran it on a real IBM Q quantum computer. When we did, we witnessed the fact that in the NISQ era, one plus one is most probably two!

Finally, note that the quantum logical AND method we built above is also signicant because one can append an X gate, which performs a logical NOT, resulting in a NAND operation. In classical cmputing, the NAND operation is a universal gate that can be used to build all other classical computing circuits. Therefore, any classical computing solution can be expressed… and we have only used 4 points of the Bloch sphere representing the total expressive power available to each qubit of a quantum computer.

Related Links

Qiskit. An open-source quantum computing framework for leveraging today’s quantum processors in research, education, and business.

Acknowledgements

The author gratefully acknowledges the thorough reviews and feedback by Luuk Ament and Robert Loredo.

Qiskit

A community to discuss Qiskit, programming quantum computers, and anything else related to quantum computing.

John Boyer

Written by

Qiskit

Qiskit

A community to discuss Qiskit, programming quantum computers, and anything else related to quantum computing.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade