bachelor_thesis/performance/generate_data_scaling_qbits.py

89 lines
2.9 KiB
Python
Raw Normal View History

2020-02-01 09:26:06 +00:00
from collections import deque
import matplotlib.pyplot as plt
import numpy as np
2020-02-10 19:10:51 +00:00
import json
from random import shuffle
2020-02-01 09:26:06 +00:00
2020-03-23 18:32:17 +00:00
from pyqcs import State, H, X, S, CZ, M, list_to_circuit
2020-02-01 09:26:06 +00:00
from pyqcs.graph.state import GraphState
from pyqcs.util.random_circuits import random_circuit
from measure_circuit import execution_statistics, measure_all
2020-02-01 09:26:06 +00:00
def S_with_extra_arg(act, i):
return S(act)
def test_scaling_qbits(state_factory
, nstart
, nstop
, ngates_per_qbit
, ncircuits
, **kwargs):
trials = deque()
2020-03-23 18:32:17 +00:00
N = (nstop - nstart)
print()
for n, qbits in enumerate(range(nstart, nstop)):
print(f"generating test data... {int(n/N * 100)} %", end="\r", flush=True)
2020-03-23 18:32:17 +00:00
measurement_circuit = list_to_circuit([M(i) for i in range(qbits)])
2020-02-01 09:26:06 +00:00
circuits = [random_circuit(qbits, ngates_per_qbit * qbits, X, H, S_with_extra_arg, CZ)
2020-03-23 18:32:17 +00:00
| measurement_circuit
2020-02-01 09:26:06 +00:00
for _ in range(ncircuits)]
2020-03-23 18:32:17 +00:00
2020-02-01 09:26:06 +00:00
state = state_factory(qbits)
for circuit in circuits:
trials.append((qbits, circuit, state))
print("generating test data... done ")
2020-02-01 09:26:06 +00:00
print("randomizing tests...", end="", flush=True)
shuffle(trials)
print(" done")
results = measure_all(trials, **kwargs)
N, avg, std_dev = execution_statistics(results, scale={i:i for i in range(nstart, nstop)}, **kwargs)
nqbits = [[i] for i in sorted(results.keys())]
N = [[i] for i in N]
avg = [[i] for i in avg]
std_dev = [[i] for i in std_dev]
return np.hstack([nqbits, N, avg, std_dev])
2020-02-01 09:26:06 +00:00
if __name__ == "__main__":
nstart = 4
2020-02-10 19:10:51 +00:00
nstop = 16
ncircuits = 50
2020-02-01 09:26:06 +00:00
ngates_per_qbit = 100
2020-02-10 19:10:51 +00:00
seed = 0xdeadbeef
2020-02-01 09:26:06 +00:00
2020-02-10 19:10:51 +00:00
np.random.seed(seed)
2020-02-01 09:26:06 +00:00
results_naive = test_scaling_qbits(State.new_zero_state
, nstart
, nstop
, ngates_per_qbit
, ncircuits
, repeat=10)
2020-02-10 19:10:51 +00:00
np.random.seed(seed)
2020-02-01 09:26:06 +00:00
results_graph = test_scaling_qbits(GraphState.new_zero_state
, nstart
, nstop
, ngates_per_qbit
, ncircuits
, repeat=10)
2020-02-10 19:10:51 +00:00
np.savetxt("qbit_scaling_naive.csv", results_naive)
print("saved naive results to qbit_scaling_naive.csv")
np.savetxt("qbit_scaling_graph.csv", results_graph)
print("saved graph results to qbit_scaling_graph.csv")
2020-02-01 09:26:06 +00:00
2020-02-10 19:10:51 +00:00
meta = {"nstart": nstart
, "nstop": nstop
, "ncircuits": ncircuits
, "ngates_per_qbit": ngates_per_qbit
, "seed": seed}
with open("qbit_scaling_meta.json", "w") as fout:
json.dump(meta, fout)
print("saved meta data to qbit_scaling_meta.json")
2020-02-01 09:26:06 +00:00