Thursday, November 23, 2023

A baby step in the wonderland of Quantum Computing...

I am of the opinion that there is no age limit for trying out new things in life.

I consider myself a perpetual student.

Today will be a memorable day for me.

I have started my journey into the world of Quantum Computing.

From almost no knowledge in college (even didn't know how to start the Basic development environment) to taking concrete steps to pick up Quantum Computing - it's a checkered journey - a lot of midnight oil is burnt - a lot of frustration, many times it became unbearable, traveled many places - but one thing always drove me - the intrinsic motivation - never did anything just for monetary gain - remained a perpetual student. I started my software journey when there was no Google. it was naturally challenging. but worth the effort...

So, here we go - my first coding in the area of Quantum Programming.

#Python libraries required for various operations
import numpy as np

#Qiskit packages used for building a quantum circuit
from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit

#Qiskit packages used to execute and simulate the quantum circuit
from qiskit import execute, Aer

#Qiskit packages used to visualize and analyze results
from qiskit.visualization import plot_histogram

#Create quantum register to store qubit
qreg_q = QuantumRegister(1, 'q')

#Create classical register to store the results
creg_c = ClassicalRegister(1, 'c')

#Initialize quantum circuit
circuit = QuantumCircuit(qreg_q, creg_c)

#Initialize all qubits to |0>
circuit.reset(qreg_q)

#Apply the Hadamard gate on the qubit
circuit.h(qreg_q)

#Apply measurement
circuit.measure(qreg_q, creg_c)

#Visualize the constructed circuit
circuit.draw()

# Use Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator
job = execute(circuit, simulator, shots=1000)

# Grab results from the job
result = job.result()

# Returns counts
counts = result.get_counts(circuit)
print("\n Output counts:",counts)

# Plot a histogram
plot_histogram(counts)


Here is the output

Output counts: {'1': 464, '0': 536}


And here goes another simple program of a basic entangler.

Enjoy...

#Python libraries required for various operations
import numpy as np

#Qiskit packages used for building a quantum circuit
from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit

#Qiskit packages used to execute and simulate the quantum circuit
from qiskit import execute, Aer


#Qiskit packages used to visualize and analyze results
from qiskit.visualization import plot_histogram

qreg_q = QuantumRegister(2, 'q')
creg_c = ClassicalRegister(2, 'c')
circuit = QuantumCircuit(qreg_q, creg_c)

circuit.reset(qreg_q[0])
circuit.reset(qreg_q[1])
circuit.h(qreg_q[0])
circuit.cx(qreg_q[0], qreg_q[1])
circuit.measure(qreg_q[0], creg_c[0])
circuit.measure(qreg_q[1], creg_c[1])

#Visualize the constructed circuit
circuit.draw()

# Use Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator
job = execute(circuit, simulator, shots=1000)

# Grab results from the job
result = job.result()

# Returns counts
counts = result.get_counts(circuit)
print("\n Output counts:",counts)

# Plot a histogram
plot_histogram(counts)

Here goes the output of the above piece of code

Output counts: {'00': 489, '11': 511}

To start something absolutely new is challenging yet satisfying. I am always motivated by intrinsic motivation. Hence the joy of learning is still there in me, a perpetual student…

No comments: