Unlocking the future
When you started with development; did this thought ever bother you – How can a class called Car honk? It did bother me. When I was introduced to object-oriented programming, I used to imagine that if I wrote that class and called the honk, I would have a car that appears momentarily and honks from within the computer. Looking back, it isn't smart to think so. But such was the gullibility of a student programmer.?
Thinking about it as an?educator now; Object-oriented programming is so powerful that we could create constructs that are so real. Such reality captures the imagination of student programmers. Let us shift our frame and change the scene.
A computer science student will know that any digital circuit will deal only in bits – 0 and 1. Programming that class called Car would be a parallel universe until they learn the role of the?compiler and the runtime. That connects those parallel universes for the students from an English statement to experienceable behaviour. Let us shift our frame and change the scene again.
How does this class car we created in let us say Python programming language correspond to the real-world problem we are trying to solve? Before you exclaim; assume the real-world challenge we are trying to solve is predicting the price of a used car in the open market.?
By now, you would have realised that we are continuously mapping from one domain to another. First from our real-world wishes to code, then our code to binaries. In the modern era, we are embarking on that journey once again. In the world of quantum computing, we are embarking on mapping concepts from our world to quantum computational units – qubits.
Quantum encoding
Is the ultimate upgrade for mapping the?real world to qubits. These qubits can exist in multiple states simultaneously thanks to the principles of superposition and entanglement. This isn’t just a minor leap; it’s akin to moving from a typewriter to a modern IDE with AI-assisted code completion. The potential for speed and efficiency is mind-boggling. You would easily connect with the phrase “grey areas of life”. Classical bits that could only store either 0 or 1 would be needed in abundance to capture the grey areas. With qubits few or even 1 would be sufficient. Thanks to the possibility to use it for representing many states simultaneously.
There are foundational encodings that are available in Qiskit. A popular framework to develop software that are to run on quantum computers. Basis, Amplitude and Angular encoding. Let us draw a quick example of each using Qiskit.
Basis encoding
This encoding is as powerful as the C/C++ language for developers. It is also the simple one to start with before the complexity of quantum physics roars. So, in this encoding we map every binary state to a qubit. So if you have a binary string that is say 4 bit long, using this encoding you will need 4 qubits. Enough talk, let us write some code –?
We install the framework
pip install qiskit
We then bring in the modules we would need for this endeavour
from qiskit import QuantumCircuit, Aer, transpile, assemble, execute
from qiskit.visualization import plot_histogram
import matplotlib.pyplot as plt
Next, we initialize the circuit which will hold the qubits
binary_string = '101010'
num_qubits = len(binary_string)
qc = QuantumCircuit(num_qubits)
We now apply X gates to this quantum circuit to do the encoding. X gate also known as Pauli-X gate flips the state of a qubit to either 0 or 1. Thus, helping us achieve this basis encoding.
for i, bit in enumerate(reversed(binary_string)):
? ? if bit == '1':
? ? ? ? qc.x(i)
In Qiskit the convention is to visualise the circuit, pictorially once it is complete.
qc.draw('mpl')
plt.show()
Until we have access to the real quantum computer we need not hold back. We could use simulators that simulate quantum computer in regular binary computer. Now that we have the quantum circuit, we could use the Aer simulator to simulate this circuit and plot the result as histogram of measurement.
# Use Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')
# Transpile the circuit for the simulator
compiled_circuit = transpile(qc, simulator)
# Assemble the circuit into a Qobj
qobj = assemble(compiled_circuit)
# Execute the circuit on the qasm simulator
result = execute(qc, backend=simulator, shots=1024).result()
# Get the counts (measurement results)
counts = result.get_counts(qc)
# Plot the histogram of the results
plot_histogram(counts)
plt.show()
We will touch upon the Amplitude and Angular encoding for a future dispatch. Till that keep vibing in all your possible states!