Skip to content

Commit 0817250

Browse files
committed
added more codes
1 parent e0506b8 commit 0817250

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

doc/Programs/OOcodes/codes.do.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,3 +2064,9 @@ for label in labels:
20642064

20652065

20662066

2067+
!split
2068+
===== Corresponding Qiskit implementations =====
2069+
2070+
!bc pycod
2071+
2072+
!ec
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from qiskit import QuantumCircuit, Aer, execute
2+
3+
class Circuit:
4+
def __init__(self, num_qubits):
5+
self.num_qubits = num_qubits
6+
self.qc = QuantumCircuit(num_qubits)
7+
8+
def add_gate(self, gate_func):
9+
gate_func(self.qc)
10+
11+
def add_measure_all(self):
12+
self.qc.measure_all()
13+
14+
def run_statevector(self):
15+
backend = Aer.get_backend('statevector_simulator')
16+
job = execute(self.qc, backend)
17+
result = job.result()
18+
statevector = result.get_statevector()
19+
return statevector
20+
21+
def run_measurement(self, shots=1024):
22+
backend = Aer.get_backend('qasm_simulator')
23+
job = execute(self.qc, backend, shots=shots)
24+
result = job.result()
25+
counts = result.get_counts()
26+
return counts
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from qiskit import QuantumCircuit
2+
import numpy as np
3+
4+
class Gates:
5+
"""Wrapper class for basic gates"""
6+
7+
@staticmethod
8+
def I(qc, qubit):
9+
qc.id(qubit)
10+
11+
@staticmethod
12+
def X(qc, qubit):
13+
qc.x(qubit)
14+
15+
@staticmethod
16+
def Y(qc, qubit):
17+
qc.y(qubit)
18+
19+
@staticmethod
20+
def Z(qc, qubit):
21+
qc.z(qubit)
22+
@staticmethod
23+
def H(qc, qubit):
24+
qc.h(qubit)
25+
26+
@staticmethod
27+
def S(qc, qubit):
28+
qc.s(qubit)
29+
30+
@staticmethod
31+
def T(qc, qubit):
32+
qc.t(qubit)
33+
34+
@staticmethod
35+
def RX(qc, qubit, theta):
36+
qc.rx(theta, qubit)
37+
38+
@staticmethod
39+
def RY(qc, qubit, theta):
40+
qc.ry(theta, qubit)
41+
42+
@staticmethod
43+
def RZ(qc, qubit, theta):
44+
qc.rz(theta, qubit)
45+
46+
@staticmethod
47+
def CNOT(qc, control, target):
48+
qc.cx(control, target)
49+
50+
@staticmethod
51+
def CZ(qc, control, target):
52+
qc.cz(control, target)
53+
54+
@staticmethod
55+
def SWAP(qc, qubit1, qubit2):
56+
qc.swap(qubit1, qubit2)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from states.bell_states import bell_state
2+
import matplotlib.pyplot as plt
3+
from qiskit.visualization import plot_histogram
4+
5+
labels = ["Phi+", "Phi-", "Psi+", "Psi-"]
6+
shots = 1000
7+
8+
for label in labels:
9+
print(f"\nBell state {label}")
10+
c = bell_state(label)
11+
12+
# Run statevector simulation
13+
state = c.run_statevector()
14+
print("Statevector:", state)
15+
16+
# Run measurements
17+
c.add_measure_all()
18+
counts = c.run_measurement(shots=shots)
19+
print("Measurement counts:", counts)
20+
21+
# Plot histogram
22+
plot_histogram(counts, title=f"Bell state {label}")
23+
plt.show()

doc/Programs/OOcodes/qiskit/readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# Modular Quantum Simulator (Qiskit Version)
3+
4+
This is a fully modular quantum simulator package implemented with Qiskit.
5+
6+
## Modules
7+
8+
- `core/gates.py` — gate factory (wrapper around Qiskit gates)
9+
- `core/circuit.py` — modular circuit builder
10+
- `states/bell_states.py` — Bell state generators
11+
- `examples/bell_test.py` — Bell state experiment
12+
13+
## Features
14+
15+
- Fully based on Qiskit's QuantumCircuit
16+
- Supports any number of qubits
17+
- Native statevector simulation and measurement
18+
- Measurement histograms
19+
- Mirrors NumPy version structure for easy comparison
20+
21+
## Dependencies
22+
23+
- Qiskit
24+
- Matplotlib
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from core.circuit import Circuit
2+
from core.gates import Gates
3+
4+
def bell_state(label="Phi+"):
5+
c = Circuit(2)
6+
7+
# Create standard Bell basis preparation
8+
Gates.H(c.qc, 0)
9+
Gates.CNOT(c.qc, 0, 1)
10+
11+
# Apply additional gates depending on label
12+
if label == "Phi+":
13+
pass
14+
elif label == "Phi-":
15+
Gates.Z(c.qc, 0)
16+
elif label == "Psi+":
17+
Gates.X(c.qc, 1)
18+
elif label == "Psi-":
19+
Gates.X(c.qc, 1)
20+
Gates.Z(c.qc, 0)
21+
else:
22+
raise ValueError("Unknown Bell state")
23+
24+
return c

0 commit comments

Comments
 (0)