-
Notifications
You must be signed in to change notification settings - Fork 0
/
sixty-part7.py
87 lines (71 loc) · 2.64 KB
/
sixty-part7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import csv
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.exceptions import QiskitError
def generate_brickwork_circuit(num_qubits, num_layers):
qc = QuantumCircuit(num_qubits)
for layer in range(num_layers):
qc.h(range(num_qubits))
if layer % 2 == 0:
for i in range(0, num_qubits - 1, 2):
qc.cx(i, i + 1)
else:
for i in range(1, num_qubits - 1, 2):
qc.cx(i, i + 1)
qc.measure_all()
return qc
def extract_subcircuit(circuit, start, end):
# Extract a sub-circuit from the full circuit
num_qubits = end - start
sub_circuit = QuantumCircuit(num_qubits)
# Apply gates from the original circuit to the sub-circuit
for gate in circuit.data:
if all(q in range(start, end) for q in gate[1]):
sub_circuit.append(gate[0], [q - start for q in gate[1]])
sub_circuit.measure_all()
return sub_circuit
def run_simulation(qc, shots=1024):
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator, optimization_level=0)
try:
result = simulator.run(compiled_circuit, shots=shots).result()
counts = result.get_counts()
return counts
except QiskitError as e:
print(f"Qiskit error during simulation: {e}")
return None
def combine_counts(counts1, counts2):
combined_counts = {}
for key in counts1:
combined_counts[key] = counts1.get(key, 0)
for key in counts2:
combined_counts[key] = combined_counts.get(key, 0) + counts2.get(key, 0)
return combined_counts
def write_counts_to_csv(counts, filename):
if counts is None:
print("No counts available to write to CSV.")
return
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Result', 'Count'])
for result, count in counts.items():
writer.writerow([result, count])
# Generate a 60-qubit brickwork circuit
original_qc = generate_brickwork_circuit(60, 5)
# Extract two 30-qubit sub-circuits
qc1 = extract_subcircuit(original_qc, 0, 30)
qc2 = extract_subcircuit(original_qc, 30, 60)
# Simulate each 30-qubit circuit
counts1 = run_simulation(qc1, shots=1024)
counts2 = run_simulation(qc2, shots=1024)
# Combine the counts from both circuits
combined_counts = combine_counts(counts1, counts2)
# Path to the output CSV file
csv_file = 'quantum_results4.csv'
# Write the results to a CSV file
write_counts_to_csv(combined_counts, csv_file)
# Print the counts
if combined_counts:
print("Combined Counts:", combined_counts)
else:
print("No results from simulation.")