Hello World! in Quantum Computing using IBM-Qiskit

Thirumalai
5 min readMar 24, 2024

--

“Hello World!” will be everyone’s first program. In the context of quantum computing, this is very different. In quantum computing, implementing superposition and entanglement is akin to writing a hello world program. This blog discusses the implementation of several fundamental quantum gates used in quantum computing. For implementation, I’m utilizing IBM’s Qiskit open-source package and the Python programming language.

Please review my earlier blogs on superposition and entanglement, as well as quantum gates and qiskit, before delving into the implementation, assuming you are familiar with or at least have a basic understanding of the Python programming language.

Qiskit is an open-source software development kit for working with quantum computers at the level of circuits, pulses, and algorithms. It provides tools for creating and manipulating quantum programs and running them on prototype quantum devices on IBM Quantum Platform or on simulators on a local computer. Before starting complete initial setup by installing qiskit, refer more here.

Quantum Gate’s :

While quantum gates are conceptually similar to traditional logic gates, their reversibility is their defining feature. The number of quantum gates is vast, but I’m going to restrict myself to the minimum necessary for this blog. For further information on quantum gates, take a look here.

Pauli X or Quantum Not gate :

The Pauli X gate is exactly similar to the NOT gate. Given an input X, the gate will flip the bit.

q[0]-quantum register, c4-classical register. X gate on qubit q[0]

When we measure Qubit q[0], we will get the desired output.

X∣0⟩ → ∣1⟩

X∣1⟩ → ∣0⟩

Hadamard Gate

This is one of the most important gates in quantum mechanics since it helps us realize the superposition of qubits. The below shows how Hadamard Gate transforms ∣0⟩ and ∣1⟩ into their respective superposition states.

H∣0⟩ → ∣+⟩

H∣1⟩ → ∣-⟩

where ∣+⟩=1/√2[∣0⟩+∣1⟩] and ∣-⟩=1/√2[∣0⟩-∣1⟩]. To understand this better, please look below at the probability distribution from IBM simulator.

∣0⟩ and ∣1⟩ were equally distributed here.

C-NOT gate :

After Hadamard, C-NOT is the other most important gate, as it helps us generate entanglement states from superposition. C-NOT is a bassically two-qubit gate. One qubit has to act as control, and the other has to act as target. The way it works is that whenever the control qubit is ∣0⟩, it does nothing to the target qubit; on the other hand, when the control qubit is ∣1⟩, it flips the bit of the target qubit; basically, it applies the “X gate to the target qubit.". In general, CNOT helps us implement and realize various quantum circuits, starting from entanglement to algorithms and much more. Refer to the to the below images from IBM Composer for more understanding.

C-NOT applied together with Hadamard to generate entanglement.
Equal probability distribution of ∣00⟩ and ∣11⟩, however ∣10⟩ and ∣01⟩ will be vanished eventually.

We have learned enough about quantum computing to write the code for our Hello World application. I indicated before that creating an entangled state is often thought of as the “hello world” of quantum computing. The source code for one of the Bell states is going to be shared in this blog. Next question: What does the bell mean?

Bell state :

The states that are maximally entangled in two qubits are referred to as Bell states. To be more specific, there are a total of four bell states, which are collectively referred to as bell states or bell pairs. Please see the image below. To read more about bell states, refer here.

Image credit to quantum computing stock exchange.

We can simulate the bell state using above mentioned X and Hadamard gate. I have included the circuit below, and I suggest you try implementing these circuits at IBM quantum computing composer.

Circuit diagram for Bell states. Image credit ResearchGate

But our aim is to generate these states using python code. Ok let’s dive into the code.

Step 1 : Import required packages

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

Step 2 : Initialize quantum and classical registers and define quantum circuit.

qubit = QuantumRegister(2)

bit = ClassicalRegister(1)

circuit = QuantumCircuit(qubit, bit)

Step 3 : Apply Hadamard gate on qubit q0 and CNOT gate as control on q0 and target at q1.

circuit.h(q0)

circuit.cx(q0,q1)

Our two qubit entanglement, or one of the four bell states, has been successfully established. That is conclusive. Congratulations on successfully completing your quantum computing “hello world” project. It may appear to be a little challenging for novices, but believe me when I say that this is nothing more than the equivalent of “print(“Hello World!”)” in Python. Furthermore, if you would like to view the circuit, utilize the piece of code that is provided below.

circuit.draw(‘mpl’)

1/√2[∣00⟩+∣11⟩] bell state

Please see the below ipython notebook for the actual code.

Here we are generating 1/√2[∣00⟩+∣11⟩] bell state.

For the above code, I would appreciate it if you could test it out on your own for additional bell states, and if you have any questions, please don’t hesitate to get in touch with me. Please leave a comment with your thoughts on this blog, and feel free to make suggestions for blogs that will be published in the future.

--

--