Skip to content

Commit

Permalink
Typing improvements (#146)
Browse files Browse the repository at this point in the history
* Fixing various typing issues

* Adding pyright check

* Fixing style

* Adding verbose flag to pyright

* Attempting to fix pyright target
  • Loading branch information
max-radin authored Jan 31, 2024
1 parent 5f92b45 commit a2b21bd
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 10 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ style-fix:
black src tests examples benchmarks benchmarks
isort --profile=black src tests examples benchmarks

pyright:
$(PYTHON) -m pyright src --pythonpath $(PYTHON)

style: flake8p mypy pyright black isort
@echo This project passes style!

test:
$(PYTHON) -m pytest -W error tests

Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ log_level="INFO"
[tool.flake8]
ignore = ['E203', 'E266', 'F401', 'W605']
max-line-length = 88

[tool.pyright]
exclude = [
"src/benchq/compilation",
"src/benchq/conversions",
"src/benchq/mlflow",
"src/benchq/problem_embeddings",
"src/benchq/problem_ingestion",
"src/benchq/resource_estimators",
"src/benchq/visualization_tools.py",
]
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dev =
numba~=0.57.0
benchq[pyscf]
benchq[azure]
pyright

pyscf =
pyscf==2.2.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class AlgorithmImplementation(Generic[T]):
error_budget: ErrorBudget
n_shots: int

@classmethod
def from_circuit(
circuit: SUPPORTED_CIRCUITS, error_budget: ErrorBudget, n_shots: int = 1
cls, circuit: SUPPORTED_CIRCUITS, error_budget: ErrorBudget, n_shots: int = 1
):
program = get_program_from_circuit(import_circuit(circuit))
return AlgorithmImplementation(program, error_budget, n_shots)
4 changes: 3 additions & 1 deletion src/benchq/algorithms/gsee/qpe_gsee.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import warnings

import numpy as np
from orquestra.integrations.cirq.conversions import to_openfermion
from orquestra.integrations.cirq.conversions import (
to_openfermion, # pyright: ignore[reportPrivateImportUsage]
)
from orquestra.quantum.operators import PauliRepresentation

from ...algorithms.data_structures import AlgorithmImplementation, ErrorBudget
Expand Down
4 changes: 3 additions & 1 deletion src/benchq/algorithms/time_evolution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
from orquestra.integrations.cirq.conversions import to_openfermion
from orquestra.integrations.cirq.conversions import (
to_openfermion, # pyright: ignore[reportPrivateImportUsage]
)
from orquestra.quantum.operators import PauliRepresentation
from pyLIQTR.QSP import gen_qsp

Expand Down
6 changes: 3 additions & 3 deletions src/benchq/compilation/transpile_to_native_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def production(self, operation: GateOperation) -> Iterable[GateOperation]:
def preprocess_gate(gate):
return (
gate.controlled(operation.gate.num_control_qubits)
if operation.gate.name == "Control"
if isinstance(operation.gate, ControlledGate)
else gate
)

Expand Down Expand Up @@ -160,7 +160,7 @@ def production(self, operation: GateOperation) -> Iterable[GateOperation]:
def preprocess_gate(gate):
return (
gate.controlled(operation.gate.num_control_qubits)
if operation.gate.name == "Control"
if isinstance(operation.gate, ControlledGate)
else gate
)

Expand Down Expand Up @@ -202,7 +202,7 @@ def production(self, operation: GateOperation) -> Iterable[GateOperation]:
def preprocess_gate(gate):
return (
gate.controlled(operation.gate.num_control_qubits)
if operation.gate.name == "Control"
if isinstance(operation.gate, ControlledGate)
else gate
)

Expand Down
2 changes: 1 addition & 1 deletion src/benchq/problem_embeddings/qpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_single_factorized_qpe_toffoli_and_qubit_cost(
eri: np.ndarray,
rank: int,
allowable_phase_estimation_error: float = 0.001,
bits_precision_coefficients: float = 10,
bits_precision_coefficients: int = 10,
) -> Tuple[int, int]:
"""Get the number of Toffoli gates and logical qubits for single factorized QPE as
described in PRX Quantum 2, 030305.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class BasicArchitectureModel(Protocol):

@property
def physical_qubit_error_rate(self) -> float:
pass
...

@property
def surface_code_cycle_time_in_seconds(self) -> float:
pass
...


@runtime_checkable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ def substrate_scheduler(graph: nx.Graph, preset: str) -> TwoRowSubstrateSchedule
cleaned_graph,
stabilizer_scheduler=greedy_stabilizer_measurement_scheduler,
)
if preset == "optimized":
elif preset == "optimized":
compiler = TwoRowSubstrateScheduler(
cleaned_graph,
pre_mapping_optimizer=fast_maximal_independent_set_stabilizer_reduction,
stabilizer_scheduler=greedy_stabilizer_measurement_scheduler,
)
else:
raise ValueError(
f"Unknown preset: {preset}. Should be either 'fast' or 'optimized'."
)
compiler.run()
end = time.time()
print("substrate scheduler took", end - start, "seconds")
Expand Down

0 comments on commit a2b21bd

Please sign in to comment.