Skip to content

Commit

Permalink
Feat: Remove all the warnings from tests. Make warnings as errors on …
Browse files Browse the repository at this point in the history
…CI and locally (#143)
  • Loading branch information
SebastianMorawiec authored Jan 8, 2024
1 parent 4c37681 commit 8be8a3e
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 51 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ github_actions-default:
style-fix:
black src tests examples benchmarks
isort --profile=black src tests examples benchmarks

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

coverage:
$(PYTHON) -m pytest -W error\
--cov=src \
--cov-fail-under=$(MIN_COVERAGE) tests \
--no-cov-on-fail \
--cov-report xml \
&& echo Code coverage Passed the $(MIN_COVERAGE)% mark!
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools", "wheel", "setuptools_scm>=6.0"]
requires = ["setuptools<=65.6.3", "wheel", "setuptools_scm>=6.0"]

# Including this section is same as 'use_scm_version=True' in setup.py
# See https://github.com/pypa/setuptools_scm for guidance
Expand Down
9 changes: 6 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ python_requires = >=3.9,!=3.9.7
install_requires =
orquestra-quantum==0.12.0
orquestra-vqa==0.9.0
orquestra-qiskit==0.13.0
orquestra-cirq==0.11.0
orquestra-qiskit==0.14.0
orquestra-cirq==0.12.0
orquestra-opt==0.10.0
networkx>=2.8.7
# Used to define and run Orquestra workflows
orquestra-sdk[all]>=0.46.0
Expand All @@ -34,7 +35,7 @@ install_requires =
more-itertools~=9.1.0
pandas==1.5.3
pyLIQTR==0.3.3
openfermion~=1.5.0
openfermion[resources]~=1.6.0
pytest # required by OpenFermion's resource_estimates module
graph-state-generation==0.2.0
juliapkg==0.1.10
Expand All @@ -44,6 +45,8 @@ install_requires =
stim==1.10
optuna==3.3.0
cvxpy==1.3.2
aiohttp~=3.9.0
setuptools<=65.6.3

[options.packages.find]
where = src
Expand Down
19 changes: 12 additions & 7 deletions src/benchq/compilation/pyliqtr_transpilation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
################################################################################
# © Copyright 2022 Zapata Computing Inc.
################################################################################
import warnings
from typing import Optional, Union

from cirq.circuits import Circuit as CirqCircuit
Expand Down Expand Up @@ -56,12 +57,16 @@ def pyliqtr_transpile_to_clifford_t(
raise ValueError("Please supply gate or circuit precision not both")

cirq_circuit = export_circuit(CirqCircuit, orquestra_circuit)
compiled_cirq_circuit = clifford_plus_t_direct_transform(
cirq_circuit,
precision=gate_precision,
circuit_precision=circuit_precision,
use_random_decomp=False,
num_rotation_gates=n_rotation_gates,
)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", message=r".* is not a rotation gate, cannot decompose."
)
compiled_cirq_circuit = clifford_plus_t_direct_transform(
cirq_circuit,
precision=gate_precision,
circuit_precision=circuit_precision,
use_random_decomp=False,
num_rotation_gates=n_rotation_gates,
)

return import_circuit(compiled_cirq_circuit, orquestra_circuit.n_qubits)
1 change: 0 additions & 1 deletion src/benchq/data_structures/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,4 @@ def find_next_highest_distance(distances, d):

def invalid_code_distance():
"""Returns the delay for invalid code distance."""
warnings.warn("Code distance is too high to be decoded.", RuntimeWarning)
return np.infty
11 changes: 4 additions & 7 deletions src/benchq/data_structures/hardware_architecture_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,15 @@ def num_optical_cross_connect_layers(
# must be used for connecting to connector switches
# Each switch has 1000 ports
# Each group of ELU + neighbor has at most 4 ELUs
warnings.warn("This output parameter has yet to be implemented.")

num_OXC_layers = None

return num_OXC_layers
# Not yet implemented
return -1

def num_ELUs_per_optical_cross_connect(
self, code_distance: int, num_communication_qubits: int
):
warnings.warn("This output parameter has yet to be implemented.")
num_ELUs_per_OXC = None
return num_ELUs_per_OXC
# Not yet implemented
return -1


DETAILED_ION_TRAP_ARCHITECTURE_MODEL = DetailedIonTrapModel()
76 changes: 62 additions & 14 deletions src/benchq/problem_ingestion/molecule_instance_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# © Copyright 2022 Zapata Computing Inc.
################################################################################
import os
import warnings
from copy import deepcopy
from dataclasses import asdict, dataclass
from typing import Iterable, List, Optional, Tuple
Expand All @@ -11,11 +12,25 @@
import urllib3 # type: ignore
from mlflow import MlflowClient # type: ignore
from openfermion import MolecularData
from openfermion.resource_estimates.molecule import (
avas_active_space,
localize,
stability,
)

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="\n\n"
" `numpy.distutils` is deprecated since NumPy 1.23.0, as a result\n",
)

# we need to disable pyscf GC as it throws around bunch of warnings
import pyscf

pyscf.gto.mole.DISABLE_GC = True

from openfermion.resource_estimates.molecule import (
avas_active_space,
localize,
stability,
)

from openfermionpyscf import PyscfMolecularData
from openfermionpyscf._run_pyscf import compute_integrals
from orquestra import sdk
Expand Down Expand Up @@ -43,11 +58,19 @@ def truncate_with_avas(
mean_field_object = stability(mean_field_object)

# localize before automatically selecting active space with AVAS
mean_field_object = localize(
mean_field_object, loc_type="pm"
) # default is loc_type ='pm' (Pipek-Mezey)

return avas_active_space(mean_field_object, ao_list=ao_list, minao=minao)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", message="The 'sym_pos' keyword is deprecated and should be"
)
mean_field_object = localize(
mean_field_object, loc_type="pm"
) # default is loc_type ='pm' (Pipek-Mezey)
active_space = avas_active_space(
mean_field_object, ao_list=ao_list, minao=minao
)

return active_space


def _create_mlflow_setup(
Expand Down Expand Up @@ -210,7 +233,12 @@ def _run_pyscf(scf_info: SCFInfo) -> Tuple[gto.Mole, scf.hf.SCF]:
if "callback" in scf_info.scf_options:
# we want to log to mlflow, AND we've defined the
# callback in scf_options
mean_field_object.run(**scf_info.scf_options)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The 'sym_pos' keyword is deprecated and should be",
)
mean_field_object.run(**scf_info.scf_options)
else:
# we want to log to mlflow, BUT haven't defined the
# callback in scf_options
Expand All @@ -231,7 +259,12 @@ def _run_pyscf(scf_info: SCFInfo) -> Tuple[gto.Mole, scf.hf.SCF]:
client.log_param(run_id, key, val)
temp_options = deepcopy(scf_info.scf_options)
temp_options["callback"] = create_mlflow_scf_callback(client, run_id)
mean_field_object.run(**temp_options)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The 'sym_pos' keyword is deprecated and should be",
)
mean_field_object.run(**temp_options)
else:
# we want to log to mlflow, but haven't defined scf_options
if not isinstance(scf_info.orq_workspace_id, str):
Expand All @@ -250,14 +283,29 @@ def _run_pyscf(scf_info: SCFInfo) -> Tuple[gto.Mole, scf.hf.SCF]:
for key, val in flat_active_dict.items():
client.log_param(run_id, key, val)
temp_options = {"callback": create_mlflow_scf_callback(client, run_id)}
mean_field_object.run(**temp_options)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The 'sym_pos' keyword is deprecated and should be",
)
mean_field_object.run(**temp_options)
else:
if scf_info.scf_options is not None:
# we don't want to run on mlflow, but we've specified scf_options
mean_field_object.run(**scf_info.scf_options)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The 'sym_pos' keyword is deprecated and should be",
)
mean_field_object.run(**scf_info.scf_options)
else:
# we don't want to run on mlflow, and haven't specified scf_options
mean_field_object.run()
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="The 'sym_pos' keyword is deprecated and should be",
)
mean_field_object.run()

if not mean_field_object.converged:
raise SCFConvergenceError()
Expand Down
12 changes: 10 additions & 2 deletions src/benchq/resource_estimation/graph/extrapolation_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ def _logarithmic_objective(params):
y_mean = np.mean(y)
total_sum_of_squares = np.sum((y - y_mean) ** 2)
residual_sum_of_squares = np.sum((y - (a_opt * np.log(x) + b_opt)) ** 2)
r_squared = 1 - (residual_sum_of_squares / total_sum_of_squares)

if total_sum_of_squares == 0:
r_squared = 1
else:
r_squared = 1 - (residual_sum_of_squares / total_sum_of_squares)

return extrapolated_point, r_squared

Expand All @@ -190,7 +194,11 @@ def _linear_objective(params):
y_mean = np.mean(y)
total_sum_of_squares = np.sum((y - y_mean) ** 2)
residual_sum_of_squares = np.sum((y - (a_opt * x + b_opt)) ** 2)
r_squared = 1 - (residual_sum_of_squares / total_sum_of_squares)

if total_sum_of_squares == 0:
r_squared = 1
else:
r_squared = 1 - (residual_sum_of_squares / total_sum_of_squares)

return extrapolated_point, r_squared

Expand Down
13 changes: 12 additions & 1 deletion src/benchq/resource_estimation/openfermion_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
# © Copyright 2023 Zapata Computing Inc.
################################################################################

import warnings
from typing import Tuple

import numpy as np
from openfermion.resource_estimates import df, sf

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="\n\n"
" `numpy.distutils` is deprecated "
"since NumPy 1.23.0, as a result\n",
)
# openfermion throws deprecation warning thru pyscf and numpy
# Could be fixed by using old setuptools, but there would be dependency conflict
from openfermion.resource_estimates import df, sf

from benchq.data_structures import (
BASIC_SC_ARCHITECTURE_MODEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ def test_hardware_resource_estimates(
num_communication_qubits_per_elu=qubits,
num_memory_qubits_per_elu=code_distance,
num_computational_qubits_per_elu=num_computational_qubits_per_elu,
num_optical_cross_connect_layers=None,
num_ELUs_per_optical_cross_connect=None,
num_optical_cross_connect_layers=-1,
num_ELUs_per_optical_cross_connect=-1,
total_num_ions=n_logical_qubits
* (num_computational_qubits_per_elu + code_distance + qubits),
total_num_communication_qubits=n_logical_qubits * qubits,
Expand Down
19 changes: 11 additions & 8 deletions tests/benchq/problem_embeddings/test_qsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# © Copyright 2023 Zapata Computing Inc.
################################################################################
"""Unit tests for benchq.problem_embeddings._qsp."""
import warnings
from collections import Counter
from typing import Mapping

Expand Down Expand Up @@ -56,14 +57,16 @@ def test_example_circuit(use_random_angles: bool):
np.random.seed(42)

# When
circuit = _qsp.get_qsp_circuit(
operator=operator,
required_precision=required_precision,
dt=dt,
tmax=tmax,
sclf=sclf,
use_random_angles=use_random_angles,
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RuntimeWarning)
circuit = _qsp.get_qsp_circuit(
operator=operator,
required_precision=required_precision,
dt=dt,
tmax=tmax,
sclf=sclf,
use_random_angles=use_random_angles,
)

# Then
# We expect this many gates being applied in the circuit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def test_decoder_model_produces_none_and_throws_warning_for_distance_too_high(
distance, expected_warning_message, decoder_data
):
decoder_model = DecoderModel.from_csv(file_path(decoder_data + ".csv"))
with warnings.catch_warnings(record=True) as w:
with pytest.warns(RuntimeWarning) as record:
assert (
get_decoder_info(BASIC_SC_ARCHITECTURE_MODEL, decoder_model, distance, 2, 3)
is None
)

assert len(w) == 1
assert issubclass(w[0].category, RuntimeWarning)
assert expected_warning_message in str(w[0].message)
assert len(record) == 1
assert issubclass(record[0].category, RuntimeWarning)
assert expected_warning_message in str(record[0].message)


def test_if_decoder_equations_have_changed():
Expand Down
19 changes: 18 additions & 1 deletion tests/benchq/resource_estimation/test_openfermion_re.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import warnings

import numpy
import numpy as np
import pytest
from openfermion.resource_estimates.molecule import pyscf_to_cas

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="\n\n"
" `numpy.distutils` is deprecated since NumPy 1.23.0, as a result\n",
)
# openfermion throws deprecation warning thru pyscf and numpy
# Could be fixed by using old setuptools, but there would be dependency conflict

# We also need to disable GC for pyscf. It causes resources warnings
import pyscf

pyscf.gto.mole.DISABLE_GC = True

from openfermion.resource_estimates.molecule import pyscf_to_cas

from benchq.data_structures import BasicArchitectureModel
from benchq.problem_ingestion.molecule_instance_generation import (
Expand Down

0 comments on commit 8be8a3e

Please sign in to comment.