QCEXP@5
QCEXP@5
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.
Single-Qubit Gates:
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:
Qiskit Implementation:-
# 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("\nResults:")
print(counts)
plot_histogram(counts)
Mitesh Singh B22-111
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'))
IMPLEMENTATION
Qiskit Implementation
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
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.