Skip to content

Commit

Permalink
Replace ResourceEstimator with update
Browse files Browse the repository at this point in the history
  • Loading branch information
Gistbatch committed Mar 22, 2024
1 parent c523de9 commit ee9d87a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
43 changes: 42 additions & 1 deletion src/resource_estimation/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 3 additions & 7 deletions src/scheduling/common/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 2 additions & 4 deletions tests/resource_estimation/cutting/test_resource_estimator.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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

0 comments on commit ee9d87a

Please sign in to comment.