Skip to content

Commit

Permalink
Add test for api
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-be committed May 16, 2024
1 parent bf855e6 commit a655b09
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/CKT_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def reconstruct_distribution(
if label not in labels:
labels.append(label)

if isinstance(labels[0], int):
subobservables = {int(key): val for key, val in subobservables.items()}
results = {int(key): val for key, val in results.items()}

qubits = {key: val.count("Z") for key, val in subobservables.items()}
observable = "".join([subobservables[l] for l in reversed(labels)])

Expand Down
2 changes: 1 addition & 1 deletion app/routes_ckt_cutting.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def gate_cut_circuit(json: dict):
return result


@blp_ckt_cutting.route("/gate-cutting/combineResultsQuokka", methods=["POST"])
@blp_ckt_cutting.route("/ctk/combineResultsQuokka", methods=["POST"])
@blp_ckt_cutting.arguments(CombineResultsRequestQuokkaSchema)
@blp_ckt_cutting.response(200, CombineResultsResponseQuokkaSchema)
def combine_results(json: dict):
Expand Down
57 changes: 56 additions & 1 deletion test/test_CKT_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from collections import defaultdict

import numpy as np
from qiskit import QuantumCircuit
from qiskit import QuantumCircuit, qasm2
from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_aer.primitives import Sampler, EstimatorV2

from app import create_app
from app.CKT_cutter import automatic_cut, reconstruct_distribution
from app.utils import counts_to_array, replace_str_index
from test.test_reconstruction import NumpyEncoder


class CutFindingTestCase(unittest.TestCase):
Expand Down Expand Up @@ -115,6 +116,60 @@ def test_reconstruction(self):
msg=f"\nExact distribution: {exact_distribution}\nReconstruced distribution: {reconstructed_dist}\nDiff: {np.abs(exact_distribution- reconstructed_dist)}",
)

def test_reconstruction_request(self):
num_qubits = 7
circuit = random_circuit(num_qubits, 6, max_operands=2, seed=1242)
cut_result = automatic_cut(circuit, 4)

individual_subcircuits = cut_result["individual_subcircuits"]
subcircuit_labels = cut_result["subcircuit_labels"]
coefficients = cut_result["coefficients"]
metadata = cut_result["metadata"]
qubit_map = metadata["qubit_map"]
subobservables = metadata["subobservables"]

sampler = Sampler(run_options={"shots": 2 ** 15})
# Retrieve results from each partition's subexperiments
results_sampler = sampler.run(individual_subcircuits).result()

results = [
{"{0:b}".format(key): val for key, val in res.items()}
for res in results_sampler.quasi_dists
]

results_dict = defaultdict(list)
for label, res in zip(subcircuit_labels, results_sampler.quasi_dists):
results_dict[label].append(res)

reconstructed_counts = reconstruct_distribution(
results_dict, coefficients, qubit_map, subobservables
)
expected_result = {
"{0:b}".format(key).zfill(num_qubits): val
for key, val in reconstructed_counts.items()
}

response = self.client.post(
"/ctk/combineResultsQuokka",
data=json.dumps(
{
"circuit": qasm2.dumps(circuit),
"subcircuit_results": results,
"cuts": {
"subcircuit_labels": subcircuit_labels,
"coefficients": [(c, w.value) for c, w in coefficients],
"metadata": metadata,
},
},
cls=NumpyEncoder,
),
content_type="application/json",
)
self.assertEqual(response.status_code, 200)

actual_result = response.get_json()["result"]
self.assertDictEqual(actual_result, expected_result)

def test_reconstruction_2(self):

circuit = QuantumCircuit(4)
Expand Down

0 comments on commit a655b09

Please sign in to comment.