Build a simple Quantum Circuit using IBM Qiskit in Python

Build a simple Quantum Circuit using IBM Qiskit in Python

Building a simple quantum circuit using IBM's Qiskit in Python is an engaging way to start exploring quantum computing. Qiskit is an open-source quantum computing framework that allows you to design quantum circuits, run experiments, and analyze data.

Step 1: Install Qiskit

To start, you need to install Qiskit. You can do this using pip:

pip install qiskit

Step 2: Import Qiskit

In your Python script or interactive environment, import the necessary components from Qiskit:

from qiskit import QuantumCircuit, execute, Aer

Step 3: Create a Quantum Circuit

Create a quantum circuit with a specific number of quantum bits (qubits) and classical bits. For a simple example, let's create a circuit with 2 qubits and 2 classical bits:

# Create a Quantum Circuit acting on a quantum register of two qubits
circuit = QuantumCircuit(2, 2)

Step 4: Add Gates to Your Circuit

You can add quantum gates to manipulate the states of qubits. For example, let's apply a Hadamard gate to the first qubit and a CNOT (controlled-NOT) gate:

# Apply a Hadamard gate to qubit 0
circuit.h(0)

# Apply a CNOT gate with control qubit 0 and target qubit 1
circuit.cx(0, 1)

Step 5: Measure the Qubits

To get output from a quantum circuit, you need to measure the qubits and store the results in the classical bits:

# Map the quantum measurement to the classical bits
circuit.measure([0,1], [0,1])

Step 6: Simulate the Circuit

Now, simulate the circuit using Qiskit��s Aer component. We'll use the Qasm simulator:

# 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("\nTotal count for 00 and 11 are:", counts)

Step 7: Visualize the Circuit and Results

Optionally, you can visualize the circuit and the results. Qiskit provides several visualization tools:

from qiskit.tools.visualization import plot_histogram, circuit_drawer

# Draw the circuit
print(circuit_drawer(circuit, output='text'))

# Plot a histogram of the results
plot_histogram(counts)

Running the Code

Execute this script in a Python environment. The output will be the quantum circuit and a histogram showing the counts of the 00 and 11 states. The Hadamard gate creates a superposition, and the CNOT entangles the two qubits, resulting in a 50-50 distribution of the 00 and 11 states when measured.

Conclusion

This is a basic example of creating and running a quantum circuit with Qiskit. The world of quantum computing is vast, and Qiskit offers a lot of tools and functionalities to explore more complex quantum algorithms and experiments.


More Tags

wordcloud2 uiedgeinsets chown tablecell redis visibility email datetime-conversion subclassing file-permissions

More Programming Guides

Other Guides

More Programming Examples