Skip to content

Commit

Permalink
Testing in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambert committed May 6, 2024
1 parent da67569 commit 0c8cad0
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 0 deletions.
File renamed without changes.
12 changes: 12 additions & 0 deletions examples/multiidx2n_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
#include "qpp/functions.hpp"
#include "qpp/internal/util.hpp"

int main() {

constexpr int ndims = 3;
int midx[ndims] = {1, 0, 1};
int dims[ndims] = {2, 2, 2};

std::cout << qpp::internal::multiidx2n(midx, ndims, dims) << std::endl;
}
50 changes: 50 additions & 0 deletions examples/single_qubit_gate_apply_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <chrono>
#include <iostream>
#include <random>
#include <vector>

#include "qpp/qpp.h"

int main(int arg, char** argv) {
using namespace qpp;
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::milliseconds;

if (arg != 3) {
std::cout
<< "pass an arguments for number of trials and number of qubits\n";
exit(EXIT_FAILURE);
}

idx number_of_trials = std::stoi(argv[1]);
idx number_of_qubits = std::stoi(argv[2]);
std::vector<cmat> gate_set = {gt.H, gt.X, gt.Y, gt.Z, gt.S, gt.T};
ket state = randket();
for (idx j = 1; j < number_of_qubits; j++) {
state = kron(state, randket());
}

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<idx> gate_dist{0, gate_set.size() - 1};
std::uniform_int_distribution<idx> qubit_dist{0, number_of_qubits - 1};

std::vector<std::pair<cmat, idx>> gates_to_apply;
for (idx j = 0; j < number_of_trials; j++) {
gates_to_apply.push_back(
std::make_pair(gate_set[gate_dist(mt)], qubit_dist(mt)));
}

duration<double, std::milli> total_time;
for (auto gate : gates_to_apply) {
auto t1 = high_resolution_clock::now();
qpp::apply(state, gate.first, {gate.second});
auto t2 = high_resolution_clock::now();
total_time += (t2 - t1);
}

std::cout << total_time.count() << std::endl;
}

54 changes: 54 additions & 0 deletions examples/two_qubit_gate_apply_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <chrono>
#include <iostream>
#include <random>
#include <vector>

#include "qpp/qpp.h"

int main(int arg, char** argv) {
using namespace qpp;
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::milliseconds;

if (arg != 3) {
std::cout
<< "pass an arguments for number of trials and number of qubits\n";
exit(EXIT_FAILURE);
}

idx number_of_trials = std::stoi(argv[1]);
idx number_of_qubits = std::stoi(argv[2]);
std::vector<cmat> gate_set = {gt.CNOT, gt.CZ, gt.SWAP, gt.RXX, gt.RYY};
ket state = randket();
for (idx j = 1; j < number_of_qubits; j++) {
state = kron(state, randket());
}

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<idx> gate_dist{0, gate_set.size() - 1};
std::uniform_int_distribution<idx> qubit_dist{0, number_of_qubits - 1};

std::vector<std::pair<cmat, std::vector<idx>>> gates_to_apply;
for (idx j = 0; j < number_of_trials; j++) {
idx a = qubit_dist(mt);
idx b = qubit_dist(mt);
while (a == b) {
b = qubit_dist(mt);
}
gates_to_apply.push_back(
std::make_pair(gate_set[gate_dist(mt)], std::vector<idx>{a, b}));
}

duration<double, std::milli> total_time;
for (auto gate : gates_to_apply) {
auto t1 = high_resolution_clock::now();
qpp::apply(state, gate.first, gate.second);
auto t2 = high_resolution_clock::now();
total_time += (t2 - t1);
}

std::cout << total_time.count() << std::endl;
}
5 changes: 5 additions & 0 deletions original_apply_function_two_qubit_15:00_23-04-2024.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
10000
2 176.545 174.842 183.596 178.394 176.159 174.526 185.764 196.628 190.345 175.515 179.988 176.591 174.93 178.4 190.857 176.199 187.605 182.297 182.269 175.799
4 189.184 181.322 183.454 182.137 183.592 203.339 247.339 179.266 176.318 200.083 181.82 182.155 198.882 183.476 180.763 185.164 181.143 185.028 179.992 192.943
8 320.075 263.933 258.172 281.16 344.28 336.755 266.493 267.674 272.531 259.63 257.905 261.764 257.426 259.785 260.897 258.122 259.11 258.94 268.418 258.673
16 23787.9 25444.5 25880.3 26085.7 26617.7 26229.1 26678.7 27656.3 28167.8 28822 29575.2 29536 28568.4 28914.4 29033.7 28474.7 28631.4 29014.6 29148.5 28824.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
10000
1 177.045 183.401 179.482 177.491 177.767 176.061 179.892 176.771 178.809 178.344 178.731 173.207 177.846 182.742 185.616 194.434 175.579 182.799 186.081 177.499
2 174.65 177.879 177.104 178.997 195.871 185.893 178.443 174.799 178.844 176.05 181.729 178.526 179.879 176.963 177.265 177.043 186.762 188.596 176.317 188.308
4 184.946 185.677 178.205 185.135 184 185.485 182.079 182.476 201.217 189.12 181.365 183.386 180.097 182.053 180.133 179.742 191.665 196.478 185.7 182.02
8 219.992 232.964 217.757 241.844 238.279 237.996 228.896 224.703 218.453 243.654 220.595 236.702 218.217 271.035 223.113 252.001 228.766 225.088 225.37 243.18
16 16772.6 16456.3 16817.3 16767.9 17139.3 17423.9 17191.1 17600.5 17299.9 17661.1 17426.8 17547.7 17839.3 17510.6 18148 18134.3 18854.3 18829.3 19560.1 19014.8
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions scripts/process_apply_test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/opt/homebrew/bin/python3
import sys
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams.update({
"text.usetex": True,
"font.family": "sans-serif",
"font.sans-serif": "Helvetica",
})
data_file_name = sys.argv[1]

file = open(data_file_name, "r")

lines = file.readlines()
number_of_trials = int(lines[0])
sizes = []
data_sets = []

for line in lines[1:]:
curr = line.strip().split(' ')
curr_set = np.array([float(n)/number_of_trials for n in curr[1:]])
sizes.append(int(curr[0]))
data_sets.append(curr_set)

data_means = np.array([np.mean(np.log2(data_set)) for data_set in data_sets])
data_stds = np.array([np.std(np.log2(data_set)) for data_set in data_sets])
plt.errorbar(np.log2(sizes), data_means, data_stds, marker='o', linestyle='')
plt.title(r"Average apply() run time for Two Qubit Gate on $2^n$ qubits"
"(10000 trials)")
plt.xlabel(r"$n$")
plt.ylabel(r"$\log_2(t)$ (ms)")
plt.show()
26 changes: 26 additions & 0 deletions scripts/run_single_qubit_gate_apply_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/opt/homebrew/bin/python3

import sys
import subprocess

experiment_name = sys.argv[1]
number_of_trials = int(sys.argv[2])
runs_per_trial = int(sys.argv[3])
max_size_exp = int(sys.argv[4])

f = open(experiment_name, 'w')
f.write(str(runs_per_trial))
f.write("\n")

sizes = [2**n for n in range(max_size_exp+1)]

for size in sizes:
f.write(str(size))
result_string = " "
for trial in range(number_of_trials):
result = subprocess.run(['./build/single_qubit_gate_apply_test',
str(runs_per_trial),
str(size)], capture_output=True, text=True)
result_string += result.stdout.strip() + " "
f.write(result_string)
f.write("\n")
26 changes: 26 additions & 0 deletions scripts/run_two_qubit_gate_apply_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/opt/homebrew/bin/python3

import sys
import subprocess

experiment_name = sys.argv[1]
number_of_trials = int(sys.argv[2])
runs_per_trial = int(sys.argv[3])
max_size_exp = int(sys.argv[4])

f = open(experiment_name, 'w')
f.write(str(runs_per_trial))
f.write("\n")

sizes = [2**n for n in range(max_size_exp+1)]

for size in sizes:
f.write(str(size))
result_string = " "
for trial in range(number_of_trials):
result = subprocess.run(['./build/two_qubit_gate_apply_test',
str(runs_per_trial),
str(size)], capture_output=True, text=True)
result_string += result.stdout.strip() + " "
f.write(result_string)
f.write("\n")

0 comments on commit 0c8cad0

Please sign in to comment.