Skip to content

Commit

Permalink
Factories and gate counts
Browse files Browse the repository at this point in the history
  • Loading branch information
mpharrigan committed Jul 18, 2024
1 parent fb6f3e4 commit 65cdb6a
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 176 deletions.
13 changes: 5 additions & 8 deletions qualtran/resource_counting/_bloq_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ class GateCounts:
Specifically, this class holds counts for the number of `TGate` (and adjoint), `Toffoli`,
`TwoBitCSwap`, `And`, clifford bloqs, single qubit rotations, and measurements.
In addition to this, the class holds a heuristic approximation for the depth of the
circuit `depth` which we compute as the depth of the call graph.
"""

t: int = 0
Expand All @@ -129,7 +127,6 @@ class GateCounts:
clifford: int = 0
rotation: int = 0
measurement: int = 0
depth: int = 0

def __add__(self, other):
if not isinstance(other, GateCounts):
Expand All @@ -143,7 +140,6 @@ def __add__(self, other):
clifford=self.clifford + other.clifford,
rotation=self.rotation + other.rotation,
measurement=self.measurement + other.measurement,
depth=self.depth + other.depth,
)

def __mul__(self, other):
Expand All @@ -155,7 +151,6 @@ def __mul__(self, other):
clifford=other * self.clifford,
rotation=other * self.rotation,
measurement=other * self.measurement,
depth=other * self.depth,
)

def __rmul__(self, other):
Expand Down Expand Up @@ -195,6 +190,11 @@ def total_t_count(
+ ts_per_rotation * self.rotation
)

def total_t_and_ccz_count(self, ts_per_rotation: int = 11) -> Dict[str, int]:
n_ccz = self.toffoli + self.cswap + self.and_bloq
n_t = self.t + ts_per_rotation * self.rotation
return {'n_t': n_t, 'n_ccz': n_ccz}


@frozen
class QECGatesCost(CostKey[GateCounts]):
Expand Down Expand Up @@ -236,12 +236,9 @@ def compute(self, bloq: 'Bloq', get_callee_cost: Callable[['Bloq'], GateCounts])
totals = GateCounts()
callees = get_bloq_callee_counts(bloq)
logger.info("Computing %s for %s from %d callee(s)", self, bloq, len(callees))
depth = 0
for callee, n_times_called in callees:
callee_cost = get_callee_cost(callee)
totals += n_times_called * callee_cost
depth = max(depth, callee_cost.depth + 1)
totals = attrs.evolve(totals, depth=depth)
return totals

def zero(self) -> GateCounts:
Expand Down
6 changes: 3 additions & 3 deletions qualtran/resource_counting/_bloq_counts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_gate_counts():
def test_qec_gates_cost():
algo = make_example_costing_bloqs()
gc = get_cost_value(algo, QECGatesCost())
assert gc == GateCounts(toffoli=100, t=2 * 2 * 10, clifford=2 * 10, depth=2)
assert gc == GateCounts(toffoli=100, t=2 * 2 * 10, clifford=2 * 10)


@pytest.mark.parametrize(
Expand All @@ -78,12 +78,12 @@ def test_qec_gates_cost():
rotations.phase_gradient.PhaseGradientUnitary(
bitsize=10, exponent=1, is_controlled=False, eps=1e-10
),
GateCounts(rotation=10, depth=1),
GateCounts(rotation=10),
],
# Recursive
[
mcmt.MultiControlPauli(cvs=(1, 1, 1), target_gate=cirq.X),
GateCounts(and_bloq=2, depth=2, measurement=2, clifford=3),
GateCounts(and_bloq=2, measurement=2, clifford=3),
],
],
)
Expand Down
1 change: 1 addition & 0 deletions qualtran/surface_code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from qualtran.surface_code.data_block import (
CompactDataBlock,
DataBlock,
FastDataBlock,
IntermediateDataBlock,
SimpleDataBlock,
Expand Down
2 changes: 1 addition & 1 deletion qualtran/surface_code/algorithm_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ def from_bloq(bloq: 'Bloq') -> 'AlgorithmSummary':
toffoli_gates=gate_count.toffoli + gate_count.and_bloq + gate_count.cswap,
rotation_gates=gate_count.rotation,
measurements=gate_count.measurement,
rotation_circuit_depth=gate_count.depth,
rotation_circuit_depth=gate_count.rotation,
algorithm_qubits=float(get_cost_value(bloq, _QUBIT_COUNT)),
)
6 changes: 3 additions & 3 deletions qualtran/surface_code/algorithm_summary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ def test_subtraction():
[mcmt.And(), AlgorithmSummary(algorithm_qubits=3, toffoli_gates=1)],
[
basic_gates.ZPowGate(exponent=0.1, global_shift=0.0, eps=1e-11),
AlgorithmSummary(algorithm_qubits=1, rotation_gates=1),
AlgorithmSummary(algorithm_qubits=1, rotation_gates=1, rotation_circuit_depth=1),
],
[
rotations.phase_gradient.PhaseGradientUnitary(
bitsize=10, exponent=1, is_controlled=False, eps=1e-10
),
AlgorithmSummary(algorithm_qubits=10, rotation_gates=10, rotation_circuit_depth=1),
AlgorithmSummary(algorithm_qubits=10, rotation_gates=10, rotation_circuit_depth=10),
],
[
mcmt.MultiControlPauli(cvs=(1, 1, 1), target_gate=cirq.X),
AlgorithmSummary(
algorithm_qubits=6, toffoli_gates=2, rotation_circuit_depth=2, measurements=2
algorithm_qubits=6, toffoli_gates=2, rotation_circuit_depth=0, measurements=2
),
],
],
Expand Down
Loading

0 comments on commit 65cdb6a

Please sign in to comment.