[go: up one dir, main page]

0% found this document useful (0 votes)
10 views5 pages

QCEXP@5

This document outlines an experiment to implement quantum gates using Qiskit and Cirq, detailing both the theory and code for various quantum gates. It includes examples of single-qubit and two-qubit gates, along with step-by-step instructions for creating and simulating quantum circuits in both frameworks. The conclusion emphasizes the importance of these gates in constructing complex quantum algorithms and the tools provided by Qiskit and Cirq for visualizing quantum computations.

Uploaded by

rahulcounsellor1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

QCEXP@5

This document outlines an experiment to implement quantum gates using Qiskit and Cirq, detailing both the theory and code for various quantum gates. It includes examples of single-qubit and two-qubit gates, along with step-by-step instructions for creating and simulating quantum circuits in both frameworks. The conclusion emphasizes the importance of these gates in constructing complex quantum algorithms and the tools provided by Qiskit and Cirq for visualizing quantum computations.

Uploaded by

rahulcounsellor1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Mitesh Singh B22-111

EXPERIMENT 5

AIM: Write a program to implement the quantum gates in Qiskit and Cirq.

THEORY:

Quantum Gates

Quantum gates are fundamental building blocks of quantum circuits, analogous to classical
logic gates in classical circuits. They manipulate qubits to perform computations in quantum
computing. Unlike classical gates, quantum gates are reversible and represented by unitary
matrices. Quantum gates can operate on single or multiple qubits, enabling complex
transformations.

Common Quantum Gates:

Single-Qubit Gates:

• Hadamard Gate (H): Creates superposition.


• Pauli-X Gate (X): Bit-flip gate.
• Pauli-Y Gate (Y): Rotation around the Y-axis.
• Pauli-Z Gate (Z): Phase-flip gate.
• Phase Gate (S): Introduces a π/2 phase.
• T Gate: Introduces a π/4 phase.

Two-Qubit Gates:

• Controlled-NOT Gate (CNOT): Flips the target qubit if the control qubit is in the |1⟩
state.
• Controlled-Z Gate (CZ): Introduces a phase of -1 if both qubits are in the |1⟩ state.
• SWAP Gate: Swaps the states of two qubits.

CODE:

Here’s the implementation of quantum gates using Qiskit:

Qiskit Implementation:-

# Importing required libraries


from qiskit import QuantumCircuit, transpile
from qiskit.visualization import plot_bloch_multivector, plot_histogram
from qiskit_aer import Aer

# Initialize a quantum circuit with 2 qubits and 2 classical bits


qc = QuantumCircuit(2, 2)
Mitesh Singh B22-111

# Apply gates
qc.h(0) # Hadamard on qubit 0
qc.cx(0, 1) # CNOT with control qubit 0 and target qubit 1
qc.z(1) # Pauli-Z on qubit 1
qc.s(0) # S gate on qubit 0
qc.swap(0,1) # Swaps qubit 0 and 1
# Measure the qubits
qc.measure([0, 1], [0, 1])

# Print the quantum circuit


print("Quantum Circuit:")
print(qc.draw())

# Simulate the circuit


simulator = Aer.get_backend('qasm_simulator')
compiled_circuit = transpile(qc, simulator)

# Run the simulation


job = simulator.run(compiled_circuit, shots=1000)
result = job.result()
counts = result.get_counts(qc)

print("\nResults:")
print(counts)
plot_histogram(counts)
Mitesh Singh B22-111

Here’s the implementation of quantum gates using Cirq:

Cirq Implementation:-

import cirq
import numpy as np

# Define qubits
q0 = cirq.GridQubit(0, 0)
q1 = cirq.GridQubit(0, 1)

# Create a circuit
circuit = cirq.Circuit()

# Add gates
circuit.append(cirq.H(q0)) # Hadamard on q0
circuit.append(cirq.CNOT(q0, q1)) # CNOT with control q0 and target q1
Mitesh Singh B22-111

circuit.append(cirq.Z(q1)) # Pauli-Z on q1
circuit.append(cirq.S(q0)) # S gate on q0
circuit.append(cirq.SWAP(q0,q1)) # Swaps q0 and q1
circuit.append(cirq.measure(q0, key='q0'))
circuit.append(cirq.measure(q1, key='q1'))

# Print the circuit


print("Quantum Circuit:")
print(circuit)

# Simulate the circuit


simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=1000)

# Print the results


print("\nResults:")
print(result.histogram(key='q0'))
print(result.histogram(key='q1'))

IMPLEMENTATION
Qiskit Implementation

1. Import Libraries: Import necessary modules from Qiskit, including


QuantumCircuit, transpile, and Aer.
2. Initialize Quantum Circuit: Create a quantum circuit with two qubits and two
classical bits using QuantumCircuit(2, 2).
3. Apply Quantum Gates:
o Apply a Hadamard gate to the first qubit: qc.h(0).
o Apply a CNOT gate with the first qubit as control and the second qubit as
target: qc.cx(0, 1).
o Apply a Pauli-Z gate to the second qubit: qc.z(1).
o Apply a Phase gate (S) to the first qubit: qc.s(0).
o Apply a Swap gate to swap the states of qubit 0 and qubit 1: qc.swap(0,1).
Mitesh Singh B22-111

4. Measure the Qubits: Measure both qubits and store the results in classical bits using
qc.measure([0, 1], [0, 1]).
5. Draw the Circuit: Print the quantum circuit using qc.draw() to visualize the gates
applied.
6. Simulate the Circuit: Run the circuit on the qasm_simulator and obtain the counts.
7. Display Results:
o Print the measurement counts using result.get_counts(qc).
o Visualize the results using plot_histogram().

Cirq Implementation

1. Import Libraries: Import necessary modules from Cirq and NumPy.


2. Define Qubits: Define two qubits using cirq.GridQubit(0, 0) and
cirq.GridQubit(0, 1).
3. Create a Circuit: Create a quantum circuit using cirq.Circuit().
4. Apply Quantum Gates:
o Apply a Hadamard gate to the first qubit: cirq.H(q0).
o Apply a CNOT gate with the first qubit as control and the second qubit as
target: cirq.CNOT(q0, q1).
o Apply a Pauli-Z gate to the second qubit: cirq.Z(q1).
o Apply a Phase gate (S) to the first qubit: cirq.S(q0).
o Apply a Swap gate to swap the states of qubit 0 and qubit 1:
cirq.SWAP(q0,q1).
5. Measure the Qubits: Measure both qubits and store the results with keys 'q0' and
'q1'.
6. Print the Circuit: Print the quantum circuit using print(circuit) to visualize the
gates applied.
7. Simulate the Circuit: Create a simulator instance using cirq.Simulator() and
simulate the circuit.
8. Display the Results: Print the measurement results for each qubit using
result.histogram.

CONCLUSION:-
In this experiment, several quantum gates were implemented and simulated using both Qiskit
and Cirq. The programs demonstrate how these gates transform the state of qubits, providing
a foundation for constructing more complex quantum circuits. Qiskit and Cirq offer intuitive
tools for visualizing and understanding the behavior of quantum gates, which are crucial for
quantum computations. The combination of single-qubit and multi-qubit gates allows for the
creation of complex quantum algorithms.

You might also like