Skip to content

Commit

Permalink
Add api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-be committed May 15, 2024
1 parent ba9a563 commit bf855e6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
29 changes: 29 additions & 0 deletions app/CKT_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
from qiskit.quantum_info import SparsePauliOp

from app.gate_cutting_reconstruct_distribution import _process_outcome_distribution
from app.model.request_combine_results import CombineResultsRequest
from app.model.request_cut_circuits import CutCircuitsRequest
from app.model.response_ckt_cut_circuits import CKTCutCircuitsResponse
from app.model.response_combine_results import CombineResultsResponse
from app.utils import (
product_dicts,
shift_bits_by_index,
find_character_in_string,
remove_bits,
counts_to_array,
)
from app.wire_cutter import _get_circuit

Expand Down Expand Up @@ -193,3 +196,29 @@ def reconstruct_distribution(
result_dict_traced_out[meas_traced_out] += val

return result_dict_traced_out


def reconstruct_result(input_dict: CombineResultsRequest, quokka_format=False):
subcircuit_results_dict = defaultdict(list)
for label, res in zip(
input_dict.cuts["subcircuit_labels"], input_dict.subcircuit_results
):
subcircuit_results_dict[label].append(res)

result = reconstruct_distribution(
subcircuit_results_dict,
input_dict.cuts["coefficients"],
input_dict.cuts["metadata"]["qubit_map"],
input_dict.cuts["metadata"]["subobservables"],
)
num_qubits = 0
for key, val in input_dict.cuts["metadata"]["subobservables"].items():
num_qubits += val.count("Z")
if not quokka_format:
result = counts_to_array(result, num_qubits)
else:
result = {
"{0:b}".format(key).zfill(num_qubits): val for key, val in result.items()
}

return CombineResultsResponse(result=result)
16 changes: 16 additions & 0 deletions app/routes_ckt_cutting.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from flask_smorest import Blueprint

from app import CKT_cutter
from app.model.request_combine_results import (
CombineResultsRequest,
CombineResultsRequestQuokkaSchema,
)
from app.model.request_cut_circuits import CutCircuitsRequestSchema, CutCircuitsRequest
from app.model.response_ckt_cut_circuits import CKTCutCircuitsResponseSchema
from app.model.response_combine_results import CombineResultsResponseQuokkaSchema

blp_ckt_cutting = Blueprint(
"ckt-cutting",
Expand All @@ -29,3 +34,14 @@ def gate_cut_circuit(json: dict):
result = CKT_cutter.cut_circuit(CutCircuitsRequest(**json))
print("result", result)
return result


@blp_ckt_cutting.route("/gate-cutting/combineResultsQuokka", methods=["POST"])
@blp_ckt_cutting.arguments(CombineResultsRequestQuokkaSchema)
@blp_ckt_cutting.response(200, CombineResultsResponseQuokkaSchema)
def combine_results(json: dict):
"""Recombine the results of the subcircuits from the ckt cut."""
print("request combine", json)
return CKT_cutter.reconstruct_result(
CombineResultsRequest(**json), quokka_format=True
)
12 changes: 0 additions & 12 deletions test/test_CKT_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,10 @@
from collections import defaultdict

import numpy as np
from circuit_knitting.cutting import (
DeviceConstraints,
OptimizationParameters,
find_cuts,
cut_wires,
expand_observables,
partition_problem,
generate_cutting_experiments,
reconstruct_expectation_values,
)
from qiskit import QuantumCircuit
from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_aer import AerSimulator
from qiskit_aer.primitives import Sampler, EstimatorV2
from qiskit_ibm_runtime.qiskit.primitives import BackendSamplerV2

from app import create_app
from app.CKT_cutter import automatic_cut, reconstruct_distribution
Expand Down

0 comments on commit bf855e6

Please sign in to comment.