Skip to content

Commit

Permalink
fix: ecp as gate not operator, feat: hardcoded qasm files to skip mqt…
Browse files Browse the repository at this point in the history
… loading
  • Loading branch information
evmckinney9 committed Oct 11, 2023
1 parent 4eda9b9 commit 4cda938
Show file tree
Hide file tree
Showing 15 changed files with 1,006 additions and 34 deletions.
38 changes: 29 additions & 9 deletions src/transpile_benchy/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def __init__(
self.logger = logger

# check that all the transpilers have different names
if len(set([t.name for t in self.transpilers])) != len(self.transpilers):
if len(set([t.name for t in self.transpilers])) != len(
self.transpilers
):
raise ValueError("Transpilers must have unique names")

# give each transpiler a reference to the metrics
Expand All @@ -63,7 +65,9 @@ def _try_transpilation(self, transpiler, circuit, run_index):
"""Attempt to transpile, returns transpiled circuit or raises Error."""
if not self._filter_circuit(circuit):
if self.logger:
self.logger.debug(f"Skipping circuit {circuit.name} due to filtering")
self.logger.debug(
f"Skipping circuit {circuit.name} due to filtering"
)
return None
if self.logger:
self.logger.debug(
Expand All @@ -78,6 +82,7 @@ def _try_transpilation(self, transpiler, circuit, run_index):
transpiled_circuit = transpiler.run(circuit)
except Exception as e:
raise ValueError("Transpiler failed") from e

return transpiled_circuit

def run_single_circuit(self, circuit: QuantumCircuit):
Expand Down Expand Up @@ -133,7 +138,10 @@ def _calculate_statistics(
# Error checking
try:
assert metric in self.metrics
assert transpiler_1 in self.transpilers and transpiler_2 in self.transpilers
assert (
transpiler_1 in self.transpilers
and transpiler_2 in self.transpilers
)
except AssertionError:
raise ValueError("Invalid metric or transpiler")

Expand All @@ -154,14 +162,17 @@ def _calculate_statistics(
total_2 += result_2.average

if result_1.average == 0:
if result_2.average == 0: # The percentages are equal, no change
if (
result_2.average == 0
): # The percentages are equal, no change
change_percentage = 0
else: # result_1 is zero but result_2 not, ie ~infinite increase
change_percentage = float("inf")

else: # Normal case, result_1.average isn't zero
change_percentage = (
(result_2.average - result_1.average) / result_1.average
(result_2.average - result_1.average)
/ result_1.average
) * 100

change_percentages.append(change_percentage)
Expand All @@ -170,7 +181,9 @@ def _calculate_statistics(
# Calculate average change and aggregate change
average_change = sum(change_percentages) / len(change_percentages)
aggregate_change = (
((total_2 - total_1) / total_1) * 100 if total_1 != 0 else float("inf")
((total_2 - total_1) / total_1) * 100
if total_1 != 0
else float("inf")
)

if metric.is_lower_better:
Expand Down Expand Up @@ -207,11 +220,15 @@ def summary_statistics(self, transpiler_1, transpiler_2) -> dict:
summary = {}

if transpiler_1 is None or transpiler_2 is None:
raise ValueError("One or both specified transpilers could not be found.")
raise ValueError(
"One or both specified transpilers could not be found."
)

for metric in self.metrics:
# calculate/store statistics for this metric on pair of transpilers
stats = self._calculate_statistics(metric, transpiler_1, transpiler_2)
stats = self._calculate_statistics(
metric, transpiler_1, transpiler_2
)
summary[metric.name] = {
"average_change": stats["average_change"],
"aggregrate_change": stats["aggregate_change"],
Expand All @@ -233,7 +250,10 @@ def __iter__(self):
(metric_name, transpiler_name, circuit_name, mean_result, trials)
"""
for metric in self.metrics:
for transpiler_name, results_by_circuit in metric.saved_results.items():
for (
transpiler_name,
results_by_circuit,
) in metric.saved_results.items():
for circuit_name, result in results_by_circuit.items():
yield (
metric.name,
Expand Down
31 changes: 31 additions & 0 deletions src/transpile_benchy/interfaces/hardcoded/adder_n4.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
OPENQASM 2.0;
include "qelib1.inc";
qreg q[4];
creg c[4];
u(pi,0,pi) q[0];
u(0,0,pi/4) q[0];
u(pi,0,pi) q[1];
u(0,0,pi/4) q[1];
cx q[0],q[1];
u(pi/2,0,pi) q[3];
cx q[2],q[3];
u(0,0,pi/4) q[2];
u(0,0,-pi/4) q[3];
cx q[2],q[3];
cx q[1],q[2];
cx q[3],q[0];
cx q[0],q[1];
u(0,0,-pi/4) q[0];
u(0,0,-pi/4) q[1];
cx q[0],q[1];
cx q[2],q[3];
u(0,0,-pi/4) q[2];
u(0,0,pi/4) q[3];
cx q[2],q[3];
u(0,0,pi/2) q[3];
cx q[3],q[0];
u(pi/2,0,pi) q[3];
measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
measure q[3] -> c[3];
176 changes: 176 additions & 0 deletions src/transpile_benchy/interfaces/hardcoded/ae_n8.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
OPENQASM 2.0;
include "qelib1.inc";
qreg eval[7];
qreg q[1];
creg meas[8];
u(pi/2,0,pi) eval[0];
u(0,0,0) eval[0];
u(pi/2,0,pi) eval[1];
u(0,0,0) eval[1];
u(pi/2,0,pi) eval[2];
u(0,0,0) eval[2];
u(pi/2,0,pi) eval[3];
u(0,0,0) eval[3];
u(pi/2,0,pi) eval[4];
u(0,0,0) eval[4];
u(pi/2,0,pi) eval[5];
u(0,0,0) eval[5];
u(pi/2,0,pi) eval[6];
u(0,0,0) eval[6];
u(0.9272952180016122,0,0) q[0];
u(0,0,0) q[0];
cx eval[0],q[0];
u(-0.9272952180016122,0,0) q[0];
cx eval[0],q[0];
u(0.9272952180016122,0,0) q[0];
u(0,0,0) q[0];
u(0,0,-pi/128) eval[0];
cx eval[1],q[0];
u(-1.8545904360032244,0,0) q[0];
cx eval[1],q[0];
u(1.8545904360032244,0,0) q[0];
u(0,0,0) q[0];
u(0,0,-pi/64) eval[1];
cx eval[2],q[0];
u(-3.7091808720064487,0,0) q[0];
cx eval[2],q[0];
u(3.7091808720064487,0,0) q[0];
u(0,0,0) q[0];
u(0,0,-pi/32) eval[2];
cx eval[3],q[0];
u(-7.4183617440128975,0,0) q[0];
cx eval[3],q[0];
u(7.4183617440128975,0,0) q[0];
u(0,0,0) q[0];
u(0,0,-pi/16) eval[3];
cx eval[4],q[0];
u(-14.836723488025795,0,0) q[0];
cx eval[4],q[0];
u(14.836723488025795,0,0) q[0];
u(0,0,0) q[0];
u(0,0,-pi/8) eval[4];
cx eval[5],q[0];
u(-29.67344697605159,0,0) q[0];
cx eval[5],q[0];
u(29.67344697605159,0,0) q[0];
u(0,0,0) q[0];
u(0,0,-pi/4) eval[5];
cx eval[6],q[0];
u(-59.34689395210318,0,0) q[0];
cx eval[6],q[0];
u(59.34689395210318,0,0) q[0];
u(pi/2,0,pi) eval[6];
cx eval[5],eval[6];
u(0,0,pi/4) eval[6];
cx eval[5],eval[6];
u(pi/2,0,pi) eval[5];
u(0,0,-pi/4) eval[6];
cx eval[4],eval[6];
u(0,0,pi/8) eval[6];
cx eval[4],eval[6];
u(0,0,-pi/4) eval[4];
cx eval[4],eval[5];
u(0,0,pi/4) eval[5];
cx eval[4],eval[5];
u(pi/2,0,pi) eval[4];
u(0,0,-pi/4) eval[5];
u(0,0,-pi/8) eval[6];
cx eval[3],eval[6];
u(0,0,pi/16) eval[6];
cx eval[3],eval[6];
u(0,0,-pi/8) eval[3];
cx eval[3],eval[5];
u(0,0,pi/8) eval[5];
cx eval[3],eval[5];
u(0,0,-pi/4) eval[3];
cx eval[3],eval[4];
u(0,0,pi/4) eval[4];
cx eval[3],eval[4];
u(pi/2,0,pi) eval[3];
u(0,0,-pi/4) eval[4];
u(0,0,-pi/8) eval[5];
u(0,0,-pi/16) eval[6];
cx eval[2],eval[6];
u(0,0,pi/32) eval[6];
cx eval[2],eval[6];
u(0,0,-pi/16) eval[2];
cx eval[2],eval[5];
u(0,0,pi/16) eval[5];
cx eval[2],eval[5];
u(0,0,-pi/8) eval[2];
cx eval[2],eval[4];
u(0,0,pi/8) eval[4];
cx eval[2],eval[4];
u(0,0,-pi/4) eval[2];
cx eval[2],eval[3];
u(0,0,pi/4) eval[3];
cx eval[2],eval[3];
u(pi/2,0,pi) eval[2];
u(0,0,-pi/4) eval[3];
u(0,0,-pi/8) eval[4];
u(0,0,-pi/16) eval[5];
u(0,0,-pi/32) eval[6];
cx eval[1],eval[6];
u(0,0,pi/64) eval[6];
cx eval[1],eval[6];
u(0,0,-pi/32) eval[1];
cx eval[1],eval[5];
u(0,0,pi/32) eval[5];
cx eval[1],eval[5];
u(0,0,-pi/16) eval[1];
cx eval[1],eval[4];
u(0,0,pi/16) eval[4];
cx eval[1],eval[4];
u(0,0,-pi/8) eval[1];
cx eval[1],eval[3];
u(0,0,pi/8) eval[3];
cx eval[1],eval[3];
u(0,0,-pi/4) eval[1];
cx eval[1],eval[2];
u(0,0,pi/4) eval[2];
cx eval[1],eval[2];
u(pi/2,0,pi) eval[1];
u(0,0,-pi/4) eval[2];
u(0,0,-pi/8) eval[3];
u(0,0,-pi/16) eval[4];
u(0,0,-pi/32) eval[5];
u(0,0,-pi/64) eval[6];
cx eval[0],eval[6];
u(0,0,pi/128) eval[6];
cx eval[0],eval[6];
u(0,0,-pi/64) eval[0];
cx eval[0],eval[5];
u(0,0,pi/64) eval[5];
cx eval[0],eval[5];
u(0,0,-pi/32) eval[0];
cx eval[0],eval[4];
u(0,0,pi/32) eval[4];
cx eval[0],eval[4];
u(0,0,-pi/16) eval[0];
cx eval[0],eval[3];
u(0,0,pi/16) eval[3];
cx eval[0],eval[3];
u(0,0,-pi/8) eval[0];
cx eval[0],eval[2];
u(0,0,pi/8) eval[2];
cx eval[0],eval[2];
u(0,0,-pi/4) eval[0];
cx eval[0],eval[1];
u(0,0,pi/4) eval[1];
cx eval[0],eval[1];
u(pi/2,0,pi) eval[0];
u(0,0,-pi/4) eval[1];
u(0,0,-pi/8) eval[2];
u(0,0,-pi/16) eval[3];
u(0,0,-pi/32) eval[4];
u(0,0,-pi/64) eval[5];
u(0,0,-pi/128) eval[6];
barrier eval[0],eval[1],eval[2],eval[3],eval[4],eval[5],eval[6],q[0];
measure eval[0] -> meas[0];
measure eval[1] -> meas[1];
measure eval[2] -> meas[2];
measure eval[3] -> meas[3];
measure eval[4] -> meas[4];
measure eval[5] -> meas[5];
measure eval[6] -> meas[6];
measure q[0] -> meas[7];
45 changes: 45 additions & 0 deletions src/transpile_benchy/interfaces/hardcoded/dj_n8.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
OPENQASM 2.0;
include "qelib1.inc";
qreg q[8];
creg c[7];
u(pi/2,0,pi) q[0];
u(pi,0,pi) q[0];
u(pi/2,0,pi) q[1];
u(pi,0,pi) q[1];
u(pi/2,0,pi) q[2];
u(pi/2,0,pi) q[3];
u(pi,0,pi) q[3];
u(pi/2,0,pi) q[4];
u(pi/2,0,pi) q[5];
u(pi,0,pi) q[5];
u(pi/2,0,pi) q[6];
u(pi,0,pi) q[6];
u(pi,0,pi) q[7];
u(pi/2,0,pi) q[7];
cx q[0],q[7];
u(pi,0,pi) q[0];
u(pi/2,0,pi) q[0];
cx q[1],q[7];
u(pi,0,pi) q[1];
u(pi/2,0,pi) q[1];
cx q[2],q[7];
u(pi/2,0,pi) q[2];
cx q[3],q[7];
u(pi,0,pi) q[3];
u(pi/2,0,pi) q[3];
cx q[4],q[7];
u(pi/2,0,pi) q[4];
cx q[5],q[7];
u(pi,0,pi) q[5];
u(pi/2,0,pi) q[5];
cx q[6],q[7];
u(pi,0,pi) q[6];
u(pi/2,0,pi) q[6];
barrier q[0],q[1],q[2],q[3],q[4],q[5],q[6],q[7];
measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
measure q[3] -> c[3];
measure q[4] -> c[4];
measure q[5] -> c[5];
measure q[6] -> c[6];
26 changes: 26 additions & 0 deletions src/transpile_benchy/interfaces/hardcoded/fredkin_n3.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];
u(pi,0,pi) q[0];
u(pi,0,pi) q[1];
cx q[2],q[1];
cx q[0],q[1];
u(0,0,pi/4) q[0];
u(0,0,-pi/4) q[1];
u(pi/2,0,pi) q[2];
u(0,0,pi/4) q[2];
cx q[2],q[1];
cx q[0],q[2];
u(0,0,pi/4) q[1];
cx q[0],q[1];
u(0,0,-pi/4) q[1];
u(0,0,-pi/4) q[2];
cx q[0],q[2];
cx q[2],q[1];
u(0,0,pi/4) q[1];
u(pi/2,0,pi) q[2];
cx q[2],q[1];
measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
Loading

0 comments on commit 4cda938

Please sign in to comment.