[go: up one dir, main page]

0% found this document useful (0 votes)
790 views25 pages

CCS359 - Quantum Computing Manual (WOL)

Uploaded by

asddinesh23
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)
790 views25 pages

CCS359 - Quantum Computing Manual (WOL)

Uploaded by

asddinesh23
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/ 25

(AUTONOMOUS)

COMPUTER SCIENCE AND ENGINEERING

CCS359 – QUANTUM COMPUTING


LABORATORY

: 2024 – 2025 (ODD SEM)

1
BONAFIDE CERTIFICATE

Name : …………………………………………………………

Degree : …………………………………………………………

Branch : …………………………………………………………

Semester : ……………Year: ……………Section: ……………

Reg. No. : …………………………………………………………

Certified that this is the bonafide record of the work done by the above student in
…………………………………………………………………………………………………

Laboratory during the academic year …………………………………

LAB-IN-CHARGE HEAD OF THE DEPARTMENT

Submitted for University Practical Examination held on……………………………………

INTERNAL EXAMINER EXTERNAL EXAMINER

2
LAB MANNERS

• Students must be present in proper dress code and wear the ID card.
• Students should enter the log-in and log-out time in the log register without fail.
• Students are not allowed to download pictures, music, videos or files without
thepermission of respective lab in-charge.
• Students should wear their own lab coats and bring observation note books to
thelaboratory classes regularly.
• Record of experiments done in a particular class should be submitted in the nextlab
class.
• Students who do not submit the record note book in time will not be allowed to
dothe next experiment and will not be given attendance for that laboratory class.
• Students will not be allowed to leave the laboratory until they complete the
experiment.
• Students are advised to switch-off the Monitors and CPU when they leave the lab.
• Students are advised to arrange the chairs properly when they leave the lab.

3
S.No Date Name of the Experiment Page Marks Staff Remarks
No. Awarded Signatu re
Single Qubit Gate Simulation -
1. Quantum Composer

Multiple Qubit Gate


Simulation - Quantum
2. Composer
Composing Simple Quantum
3. Circuits with Q-Gates and
Measuring Output into
Classical Bits
IBM Qiskit Platform
4. Introduction

Implementation of Shor's
5. Algorithm

6. Implementation of Grover's
Algorithm
7. Implementation of Deutsch's
Algorithm

8. Implementation of
Deutsch-Jozsa Algorithm

11
Dhirajlal Gandhi College of Technology

Experiment Score /10


EX. NO.:1 Date of
Completion Additional Credits

Single Qubit Gate Simulation - Quantum Composer

AIM

To simulate the operation of single-qubit gates (X, Z, H gates) using a quantum


composer and observe the resulting quantum state.

ALGORITHM

1. Start with an initial quantum state, e.g., |0⟩.


2. Apply the X (NOT) gate: This flips the qubit from |0⟩ to |1⟩ or vice versa.
3. Apply the Z gate: This adds a phase flip to the qubit.
4. Apply the H (Hadamard) gate: This puts the qubit into superposition.
5. Measure the quantum state to observe the result.

PROGRAM

from qiskit import QuantumCircuit, Aer, execute


from qiskit.visualization import plot_bloch_multivector

# Create a Quantum Circuit with 1 qubit


qc = QuantumCircuit(1)

# Apply X gate (NOT Gate)


qc.x(0)
1

# Apply Z gate (Phase Flip Gate)


Dhirajlal Gandhi College of Technology

qc.z(0)

# Apply H gate (Hadamard Gate)


qc.h(0)

# Visualize the circuit


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

# Simulate the quantum state


simulator = Aer.get_backend('statevector_simulator')
result = execute(qc, simulator).result()
state = result.get_statevector()

# Visualize the final state on Bloch Sphere


print("Final Quantum State:")
print(state)
plot_bloch_multivector(state)

OUTPUT

Quantum Circuit:
┌───┐┌───┐┌───┐
q_0 : ┤ X ├┤ Z ├┤ H ├
└───┘└───┘└───┘

Final Quantum State:


[ 0.70710678+0.j -0.70710678+0.j]

2
Dhirajlal Gandhi College of Technology

RESULT
The single qubit gates were successfully simulated using the quantum composer (Qiskit), and the final
state was observed.
3
Dhirajlal Gandhi College of Technology

Experiment Score /10


EX. NO.:2 Date of
Completion Additional Credits

Multiple Qubit Gate Simulation - Quantum Composer

AIM
To simulate multi-qubit gate operations such as CNOT and Toffoli using a quantum composer.

ALGORITHM

1. Initialize a two-qubit system in the |00⟩ state.


2. Apply a Hadamard gate to the first qubit to create a superposition.
3. Use the CNOT gate to entangle the two qubits.
4. (Optional) For a three-qubit system, apply a Toffoli gate for controlled operations.
5. Measure the output quantum state.

PROGRAM

from qiskit import QuantumCircuit, Aer, execute


from qiskit.visualization import plot_histogram

# Create a Quantum Circuit with 2 qubits


qc = QuantumCircuit(2)

# Apply H gate to the first qubit


qc.h(0)

# Apply CNOT gate (control: qubit 0, target: qubit 1)


qc.cx(0, 1)

# Visualize the circuit


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

# Simulate the measurement


qc.measure_all()
simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, simulator, shots=1024).result()
counts = result.get_counts()
4
Dhirajlal Gandhi College of Technology

# Plot histogram of the measurement results

print("Measurement Results:")
print(counts)
plot_histogram(counts)

OUTPUT

Quantum Circuit:

┌───┐
q_0: ┤ H ├──■──
└───┘ ┌─┴─┐
q_1: ─────┤ X ├
└───┘
q_0: ─ |M| ──────
q_1: ─ |M| ──────

Measurement Results:

{'00': 512, '11': 512}

Histogram:

| 00 | ██████████ 50%
| 11 | ██████████ 50%

RESULT

The multiple qubit gates (CNOT) were simulated successfully, and the entangled state was
observed.
5
Dhirajlal Gandhi College of Technology

Experiment Score /10


EX. NO.3 Date of
Completion Additional Credits

Composing Simple Quantum Circuits with Q-Gates and


Measuring Output into Classical Bits

AIM
To compose a simple quantum circuit using quantum gates and measure the output into classical bits.

ALGORITHM

1. Initialize a quantum circuit with one qubit and one classical bit.
2. Apply a series of gates (X, H, Z, etc.) to manipulate the quantum state.
3. Measure the quantum state and store the result in a classical bit.
4. Execute the circuit and observe the results.

PROGRAM:

from qiskit import QuantumCircuit, Aer, execute


from qiskit.visualization import plot_histogram

# Create a Quantum Circuit with 1 qubit and 1 classical bit


qc = QuantumCircuit(1, 1)

# Apply a series of quantum gates


qc.h(0) # Apply Hadamard gate
qc.z(0) # Apply Z gate
qc.x(0) # Apply X gate

# Measure the quantum state into the classical bit


qc.measure(0, 0)

# Visualize the circuit


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

# Execute the circuit


simulator = Aer.get_backend('qasm_simulator')
6
Dhirajlal Gandhi College of Technology
result = execute(qc, simulator, shots=1024).result()
counts = result.get_counts()

# Display measurement results


print("Measurement Results:")
print(counts)
plot_histogram(counts)

OUTPUT:
Quantum Circuit:
┌───┐┌───┐┌───┐ ┌─┐
q_0: ┤ H ├┤ Z ├┤ X ├┤M├
└───┘└───┘└───┘ └─┘
c_0: ═════════════════════

Measurement Results:
{'0': 512, '1': 512}

Histogram:
| 0 | ██████████ 50%
| 1 | ██████████ 50%

RESULT
The quantum circuit was successfully composed using multiple gates, and the output was
measured into classical bits.

7
Dhirajlal Gandhi College of Technology

Experiment Score /10


EX. NO.4 Date of
Completion Additional Credits

IBM Qiskit Platform Introduction

AIM
To understand the IBM Qiskit platform and implement a basic quantum circuit using Qiskit.

ALGORITHM

1. Install Qiskit using pip:


2. pip install qiskit
3. Import the required Qiskit modules.
4. Create a quantum circuit using QuantumCircuit().
5. Add quantum gates and measure the output.
6. Simulate or run the circuit on a quantum computer backend.

PROGRAM

from qiskit import QuantumCircuit, Aer, execute


from qiskit.visualization import plot_bloch_multivector

# Create a Quantum Circuit with 1 qubit


qc = QuantumCircuit(1)

# Apply an H gate
qc.h(0)

# Visualize the circuit


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

# Simulate the state vector


simulator = Aer.get_backend('statevector_simulator')
result = execute(qc, simulator).result()
state = result.get_statevector()

# Visualize the quantum state


print("Quantum State:")
8
Dhirajlal Gandhi College of Technology
print(state)

plot_bloch_multivector(state)

OUTPUT

Quantum Circuit:
┌───┐
q_0: ┤ H ├
└───┘

Quantum State:

[ 0.70710678+0.j 0.70710678+0.j]

RESULT
The IBM Qiskit platform was introduced, and a basic quantum circuit was successfully
implemented and visualized.

9
Dhirajlal Gandhi College of Technology

Experiment Score /10


EX. NO.:5 Date of
Completion Additional Credits

Implementation of Shor's Algorithm

AIM
To implement Shor’s algorithm for integer factorization using quantum circuits.

ALGORITHM
1. Choose a composite number NNN (e.g., N=15N = 15N=15).
2. Randomly select a number aaa such that 1<a<N1 < a < N1<a<N.
3. Check if gcd(a,N)≠1\text{gcd}(a, N) \neq 1gcd(a,N) =1:
o If true, gcd(a,N)\text{gcd}(a, N)gcd(a,N) is a factor of NNN.
4. Apply quantum operations to determine the period rrr of axmod Na^x \mod NaxmodN.
5. Compute the factors using gcd(ar/2±1,N)\text{gcd}(a^{r/2} \pm 1, N)gcd(ar/2±1,N).
6. Verify the factors.

PROGRAM

from qiskit.algorithms import Shor


from qiskit import Aer

# Set the number to be factored


N = 15

# Create a backend simulator


backend = Aer.get_backend('qasm_simulator')

# Run Shor's algorithm


shor = Shor()
result = shor.factor(N, backend)

# Print the factors


print("Factors of", N, "are:", result.factors)

10
Dhirajlal Gandhi College of Technology

OUTPUT

Factors of 15 are: [[3, 5]]

RESULT
Shor’s algorithm was successfully implemented to factorize a composite number NNN using Qiskit.

11
Dhirajlal Gandhi College of Technology

Experiment Score /10


EX. NO.:6 Date of
Completion Additional Credits

Implementation of Grover's Algorithm

AIM
To implement Grover's search algorithm for finding a marked item in an unsorted database.

ALGORITHM

1. Define a quantum circuit with nnn qubits for 2n2^n2n database elements.
2. Initialize the qubits into an equal superposition state using Hadamard gates.
3. Apply the oracle to mark the desired element.
4. Apply the Grover diffusion operator to amplify the marked element's probability.
5. Repeat steps 3-4 for N\sqrt{N}N iterations.
6. Measure the qubits to identify the marked element.

PROGRAM

from qiskit import QuantumCircuit, Aer, execute


from qiskit.circuit.library import GroverOperator
from qiskit.visualization import plot_histogram

# Define the oracle for the marked state (e.g., |11⟩)


oracle = QuantumCircuit(2)
oracle.cz(0, 1)
oracle = oracle.to_gate()
oracle.name = "Oracle"

# Create a Grover operator


grover_op = GroverOperator(oracle)
43
Dhirajlal Gandhi College of Technology

# Create the quantum circuit


qc = QuantumCircuit(2, 2)

# Apply Hadamard gates to initialize superposition


qc.h([0, 1])

# Apply Grover's algorithm (oracle + diffusion operator)


qc.append(grover_op, [0, 1])

# Measure the qubits


qc.measure([0, 1], [0, 1])

# Visualize the circuit


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

# Execute the circuit


simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, simulator, shots=1024).result()
counts = result.get_counts()

# Display the results


print("Measurement Results:")
print(counts)
plot_histogram(counts)

44
Dhirajlal Gandhi College of Technology
OUTPUT

Quantum Circuit:
┌───┐ ┌─────────┐ ┌───┐ ┌─┐
q_0: ┤ H ├───┤ Oracle ├───┤ H ├┤ M ├
└───┘ └─────────┘ └───┘ └─┘

q_1: ┤ H ├───┤ Oracle ├───┤ H ├┤M├


└───┘ └─────────┘ └───┘

c_0: ═══════════════════════════════════════

c_1: ═══════════════════════════════════════

Measurement Results:

{'11': 1020, '00': 4}

RESULT
Grover’s search algorithm was successfully implemented, demonstrating its ability to locate a
marked item in an unsorted database efficiently.

45
Dhirajlal Gandhi College of Technology

Experiment Score /10


EX. NO.:7 Date of
Completion Additional Credits

Implementation of Deutsch's Algorithm

AIM
To implement Deutsch's algorithm to determine whether a given quantum function is constant or balanced
using a single evaluation.

ALGORITHM

1. Create a quantum circuit with 2 qubits and 1 classical bit.


2. Initialize the first qubit (∣q0⟩|q_0\rangle∣q0⟩) to ∣0⟩|0\rangle∣0⟩ and the second qubit (∣q1⟩|q_1\rangle∣q1
⟩) to ∣1⟩|1\rangle∣1⟩.
3. Apply Hadamard gates to both qubits to create a superposition.
4. Apply the oracle that implements the given function f(x)f(x)f(x).
5. Apply another Hadamard gate to the first qubit (∣q0⟩|q_0\rangle∣q0⟩).
6. Measure the first qubit (∣q0⟩|q_0\rangle∣q0⟩):
• Output 000: Function f(x)f(x)f(x) is constant.
• Output 111: Function f(x)f(x)f(x) is balanced.

PROGRAM

from qiskit import QuantumCircuit, Aer, execute

# Define the Deutsch oracle for a constant function (e.g., f(x) = 0)


def constant_oracle():
oracle = QuantumCircuit(2)
return oracle.to_gate()

# Define the Deutsch oracle for a balanced function (e.g., f(x) = x)


49
Dhirajlal Gandhi College of Technology
def balanced_oracle():
oracle = QuantumCircuit(2)
oracle.cx(0, 1) # Controlled-X (CNOT) gate
return oracle.to_gate()

# Select the oracle type


oracle = balanced_oracle() # Replace with constant_oracle() for a constant function
oracle.name = "Oracle"

# Create the quantum circuit


qc = QuantumCircuit(2, 1)

# Step 1: Initialize the qubits


qc.x(1) # Set the second qubit to |1>
qc.h([0, 1]) # Apply Hadamard gates

# Step 2: Apply the oracle


qc.append(oracle, [0, 1])

# Step 3: Apply Hadamard to the first qubit


qc.h(0)

# Step 4: Measure the first qubit


qc.measure(0, 0)

# Visualize the circuit


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

# Execute the circuit


simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, simulator, shots=1024).result()
counts = result.get_counts()

# Display the results


print("Measurement Results:")
print(counts)

50
Dhirajlal Gandhi College of Technology
OUTPUT

Quantum Circuit:

┌───┐ ┌───┐ ┌───┐ ┌─┐


q_0: ┤ X ├───┤ H ├───┤ Oracle ├───┤ H ├┤M├
└───┘ └───┘ └───┘ └───┘└─┘

q_1: ┤ H ├───┤ Oracle ├───┤ H ├──────┤M├


└───┘ └───┘ └───┘

c_0: ═══════════════════════════════════════

Measurement Results:

• If the oracle is constant, the outcome of the first qubit after applying the Hadamard gate will be 0
Then the Measurement Results:

{'0': 1024}

• If the oracle is balanced, the first qubit will likely collapse to 1 after applying the Hadamard gate
Then the Measurement Results:

{'1': 1024}

RESULT
Deutsch's algorithm was successfully implemented to determine whether a given quantum function is
constant or balanced.
51
Dhirajlal Gandhi College of Technology

Experiment Score /10


EX. NO.:8 Date of
Completion Additional Credits

Implementation of Deutsch-Jozsa Algorithm

AIM
To implement the Deutsch-Jozsa algorithm to determine if a given function is constant or balanced using a
quantum circuit.

ALGORITHM
1) Create a quantum circuit with n+1n+1n+1 qubits and nnn classical bits.
2) Initialize nnn qubits to ∣0⟩|0\rangle∣0⟩ and the last qubit to ∣1⟩|1\rangle∣1⟩.
3) Apply Hadamard gates to all qubits.
4) Apply the oracle representing the function f(x)f(x)f(x).
5) Apply Hadamard gates to the nnn input qubits.
6) Measure the nnn input qubits:
• Output 0...00...00...0: Function is constant.
• Any other result: Function is balanced.

PROGRAM

from qiskit import QuantumCircuit, Aer, execute

# Define the Deutsch-Jozsa oracle for a constant function (e.g., f(x) = 0)


def constant_oracle(n):
oracle = QuantumCircuit(n + 1)
return oracle.to_gate()

# Define the Deutsch-Jozsa oracle for a balanced function (e.g., f(x) = x1 XOR x2)
def balanced_oracle(n):
oracle = QuantumCircuit(n + 1)
for qubit in range(n):
oracle.cx(qubit, n)
return oracle.to_gate()

# Set the number of qubits (n)


n=2
52
Dhirajlal Gandhi College of Technology

# Select the oracle type


oracle = balanced_oracle(n) # Replace with constant_oracle(n) for a constant function
oracle.name = "Oracle"

# Create the quantum circuit


qc = QuantumCircuit(n + 1, n)

# Step 1: Initialize the qubits


qc.x(n) # Set the last qubit to |1>
qc.h(range(n + 1)) # Apply Hadamard gates to all qubits

# Step 2: Apply the oracle


qc.append(oracle, range(n + 1))

# Step 3: Apply Hadamard to the input qubits


qc.h(range(n))

# Step 4: Measure the input qubits


qc.measure(range(n), range(n))

# Visualize the circuit


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

# Execute the circuit


simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, simulator, shots=1024).result()
counts = result.get_counts()

# Display the results


print("Measurement Results:")
print(counts)

53
Dhirajlal Gandhi College of Technology

OUTPUT

Quantum Circuit:
┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐┌─┐
q_0: ┤ H ├───┤ H ├───┤ X ├┤ H ├───┤M├
├───┤ ├───┤ └───┘ └───┘└─┘

q_1: ┤ H ├───┤ H ├────┤ H ├──────┤M├


└───┘ └───┘ └───┘

c_0: ═══════════════════════════════════════

c_1: ═══════════════════════════════════════

Measurement Results:

• If the oracle is constant (i.e., f(x) = 0 for all inputs), the output will be 0 for all measurements.
Then the Measurement Results:
{'00': 1024}

• If the oracle is balanced (i.e., f(x) = x1 XOR x2 ), the output will be 00 with certainty
Then the Measurement Results:
{'00': 1024}

RESULT
The Deutsch-Jozsa algorithm was successfully implemented, demonstrating its ability to classify
functions as constant or balanced with a single evaluation.
54
51

You might also like