Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
huafei1137 committed Sep 11, 2023
1 parent 4734df4 commit 9a87190
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 10 deletions.
12 changes: 6 additions & 6 deletions data/test_benchmark/hhl_n7.qasm
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,10 @@ rz(1.5708) q[5];
rz(2.24139) q[0];
sx q[0];
rz(1.5708) q[0];
measure q[2] -> meas[0];
measure q[3] -> meas[1];
measure q[1] -> meas[2];
measure q[4] -> meas[3];
measure q[5] -> meas[4];
measure q[0] -> meas[5];
measure q[0] -> meas[0];
measure q[1] -> meas[1];
measure q[2] -> meas[2];
measure q[3] -> meas[3];
measure q[4] -> meas[4];
measure q[5] -> meas[5];
measure q[6] -> meas[6];
8 changes: 4 additions & 4 deletions data/test_benchmark/sat_n11.qasm
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ h v[3];
h v[4];

// Measurements
measure v[1] -> m[0];
measure v[2] -> m[1];
measure v[3] -> m[2];
measure v[4] -> m[3];
measure v[0] -> m[0];
measure v[1] -> m[1];
measure v[2] -> m[2];
measure v[3] -> m[3];
2 changes: 2 additions & 0 deletions test/compare_summary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

===== Processing ../data/test_benchmark/qaoa_n6.qasm: =====
Empty file added test/temp_cpp_output.txt
Empty file.
76 changes: 76 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import sys
from qiskit import Aer, QuantumCircuit, transpile, assemble
from qiskit.test.mock import FakeToronto

def compare_counts(counts1, counts2, threshold=0.005):
"""Compare two count dictionaries. Consider them the same if the absolute difference
for each key is less than the threshold times the total shots."""
total_shots = sum(counts1.values())
diff_rate = 0
for key in set(counts1.keys()).union(counts2.keys()): # Get all keys from both dicts
diff = abs(counts1.get(key, 0) - counts2.get(key, 0))
diff_rate += diff / total_shots
print(f'the fidelity difference is {diff_rate}')
if diff_rate > threshold:
return False
return True

def run_circuit_from_qasm_file(qasm_file_path, backend):
# Load QASM file into a QuantumCircuit
with open(qasm_file_path, 'r') as qasm_file:
qasm_content = qasm_file.read()
circuit = QuantumCircuit.from_qasm_str(qasm_content)

# Transpile the circuit for the specified backend
transpiled_circuit = transpile(circuit, backend=backend,routing_method='sabre')

# Use Aer's QasmSimulator for simulation
simulator = Aer.get_backend('qasm_simulator')

# Assemble the circuits into a Qobj that can be executed
# qobj = assemble(transpiled_circuit)

# Execute the circuit on the simulator
result = simulator.run(transpiled_circuit,shots=8000000).result()

# Return the result counts
return result.get_counts(transpiled_circuit)
def run_circuit_from_qasm_file2(qasm_file_path, backend):
# Load QASM file into a QuantumCircuit
with open(qasm_file_path, 'r') as qasm_file:
qasm_content = qasm_file.read()
circuit = QuantumCircuit.from_qasm_str(qasm_content)

# Transpile the circuit for the specified backend
simulator = Aer.get_backend('qasm_simulator')


# Execute the circuit on the simulator
result = simulator.run(circuit,shots=8000000).result()

# Return the result counts
return result.get_counts(circuit)

def main():
# Check for input arguments
if len(sys.argv) != 2:
print("Usage: python script_name.py <path_to_qasm_file>")
sys.exit(1)

# Use FakeToronto backend for transpilation
backend = FakeToronto()

# Get results for both circuits
cmd_line_counts = run_circuit_from_qasm_file(sys.argv[1], backend)
build_output_counts = run_circuit_from_qasm_file2("/Users/huaf990/Library/CloudStorage/OneDrive-PNNL/Documents/sim_test/qasmtrans/build/output.qasm", backend)

# Compare the results
if compare_counts(cmd_line_counts, build_output_counts):
print("The results of the two circuits are considered the same!")
else:
print("The results of the two circuits differ!")
print("Command line circuit results:", cmd_line_counts)
print("build/output.qasm circuit results:", build_output_counts)

if __name__ == "__main__":
main()
27 changes: 27 additions & 0 deletions test/validation_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

dir="../data/test_benchmark"
echo "" > compare_summary.txt

for file in $(find "$dir" -type f -name "*.qasm")
do
# Skip files containing "vqe" in their name
if [[ $file == *vqe* ]] || [[ $file == *qec* ]] || [[ $file == *bwt* ]]; then
continue
fi

echo "===== Processing $file: ====="

# Execute the C++ program and save the output to a temporary file
./../build/qasmtrans -i $file -c ../data/devices/ibmq_toronto.json -o ../build/output.qasm -v 1 -limited > temp_cpp_output.txt
echo "===== Processing $file: =====" >> compare_summary.txt
# Execute the Python script and save the output to a temporary file
python3 test.py $file >> compare_summary.txt

echo "===== end ======="
done

# Cleanup temporary files
rm temp_cpp_output.txt

echo "Finish all test files, result store in the compare_summary.txt"

0 comments on commit 9a87190

Please sign in to comment.