diff --git a/src/resource_estimation/__init__.py b/src/resource_estimation/__init__.py index 376563d..774afc9 100644 --- a/src/resource_estimation/__init__.py +++ b/src/resource_estimation/__init__.py @@ -1,5 +1,46 @@ +from dataclasses import dataclass +import numpy as np + from .running import estimate_runtime from .noise import estimate_noise from .device import predict_device -from .cutting.src import ResourceEstimator, GroupingMethod +try: + from .cutting.src import ResourceEstimator +except ImportError: + + @dataclass + class Resource: + """Dataclass for the resources required to evaluate a cut quantum circuit.""" + + kappa: int + gate_groups: dict[str, list[int]] + n_samples: int + n_circuits: int + + def __repr__(self) -> str: + if 1 == self.n_circuits: + return ( + f"{self.n_circuits} job with " + + f"{round(self.n_samples / self.n_circuits)} shots." + ) + return ( + f"{self.n_circuits} jobs with " + + f"{round(self.n_samples / self.n_circuits)} shots each." + ) + + class ResourceEstimator: + def __init__(self, circuit) -> None: + self.circuit = circuit + + def resource(self, binary, epsilon, delta) -> Resource: + counter = 0 + indices = [idx for idx, value in enumerate(binary) if value == 0] + hoefdings = 2 / epsilon**2 * np.log(2 / delta) + for gate in self.circuit.data: + + if not all( + self.circuit.find_bit(qubit).index in indices for qubit in gate[1] + ): + counter += 1 + return Resource(3, {}, 3**2 * hoefdings, 6**counter) diff --git a/src/scheduling/common/partition.py b/src/scheduling/common/partition.py index 3a50140..2774c14 100644 --- a/src/scheduling/common/partition.py +++ b/src/scheduling/common/partition.py @@ -4,7 +4,7 @@ from operator import mul from src.tools import generate_subcircuit -from src.resource_estimation import ResourceEstimator, GroupingMethod +from src.resource_estimation import ResourceEstimator from .estimate import estimate_noise_proxy, estimate_runtime_proxy from .types import CircuitProxy @@ -75,9 +75,7 @@ def _bipartition( """Bipartitions the circut, giving back both parts, not to be cut further.""" estimator = ResourceEstimator(circuit.origin) # TODO find grouping method once its supported by knitting toolbox - resource = estimator.resource( - binary=binary_partition, epsilon=0.1, delta=0.1, method=GroupingMethod.SIMPLE - ) + resource = estimator.resource(binary=binary_partition, epsilon=0.1, delta=0.1) n_shots = resource.n_samples // (2 * resource.n_circuits) proxies = [] for _ in range(resource.n_circuits): @@ -122,9 +120,7 @@ def _partition( """ estimator = ResourceEstimator(circuit.origin) # TODO find grouping method once its supported by knitting toolbox - resource = estimator.resource( - binary=binary_partition, epsilon=0.1, delta=0.1, method=GroupingMethod.SIMPLE - ) + resource = estimator.resource(binary=binary_partition, epsilon=0.1, delta=0.1) n_shots = resource.n_samples // (2 * resource.n_circuits) proxies = [] for _ in range(resource.n_circuits): diff --git a/tests/resource_estimation/cutting/test_resource_estimator.py b/tests/resource_estimation/cutting/test_resource_estimator.py index 9b5b8be..d9dbb0a 100644 --- a/tests/resource_estimation/cutting/test_resource_estimator.py +++ b/tests/resource_estimation/cutting/test_resource_estimator.py @@ -1,7 +1,7 @@ """Test the resource estimation.""" -from src.resource_estimation import ResourceEstimator, GroupingMethod from tests.helpers import create_quantum_only_ghz +from src.resource_estimation import ResourceEstimator def test_resource_estimator() -> None: @@ -12,8 +12,6 @@ def test_resource_estimator() -> None: circuit = create_quantum_only_ghz(10) estimator = ResourceEstimator(circuit) partition = [0] * 5 + [1] * 5 - resource = estimator.resource( - binary=partition, epsilon=0.1, delta=0.1, method=GroupingMethod.SIMPLE - ) + resource = estimator.resource(binary=partition, epsilon=0.1, delta=0.1) assert resource.n_circuits * 2 == 12 assert resource.n_samples // (2 * resource.n_circuits) == 898