Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: onboard to use ruff #34

Closed
wants to merge 46 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
de46e0b
change: nobraket branch
kshyatt-aws Aug 8, 2024
0cf7bb4
change: Remove threading tests thanks to new pythoncall version
kshyatt-aws Aug 9, 2024
6239671
fix: make tests compatible with xdist
Aug 15, 2024
784c225
fix: Use proper BraketSimulator.jl version
kshyatt-aws Aug 19, 2024
615a471
Use a `ProcessPoolExecutor` to talk to Julia (#30)
kshyatt-aws Aug 21, 2024
9882924
fix: Use new BraketSimulator.jl version
kshyatt-aws Aug 21, 2024
38ba637
fix: Force a Julia registry update in CI to catch new versions
kshyatt-aws Aug 21, 2024
2e8fb1e
update tests (#31)
AbeCoull Aug 22, 2024
0d4d4c4
change: Use multiprocessing Pool rather than ProcessPoolExecutor
kshyatt-aws Aug 21, 2024
80c67f7
fix: No sigterm screaming
kshyatt-aws Aug 22, 2024
05870ba
fix: Force close and join at exit
kshyatt-aws Aug 22, 2024
58ccd81
fix: Faster
kshyatt-aws Aug 23, 2024
8fd3340
fix: handle julia error in serializable way
kshyatt-aws Aug 26, 2024
5f0a2d1
change: Add benchmark workflow to CI
kshyatt-aws Aug 26, 2024
88a55d4
change: Point to latest unblocking branch
kshyatt-aws Aug 26, 2024
7140d2d
fix: linting
kshyatt-aws Aug 26, 2024
509479e
fix: Don't turn off juliapkg in tox
kshyatt-aws Aug 26, 2024
043a4af
infra: onboard to use ruff
Aug 27, 2024
86d2e68
fix: move top level lint configuration
Aug 27, 2024
2eb5db1
remove unused imports
Aug 27, 2024
f550cab
Update src/braket/simulator_v2/julia_workers.py
kshyatt-aws Aug 27, 2024
3e244c1
Update src/braket/simulator_v2/julia_workers.py
kshyatt-aws Aug 27, 2024
3c6735b
fix: one-line pip in benchmark
kshyatt-aws Aug 27, 2024
716bd9c
fix: typo
kshyatt-aws Aug 27, 2024
b62a8f9
fix: Use benchmark-json option correctly
kshyatt-aws Aug 27, 2024
95d6292
Merge branch 'ksh/speed' into had_a_ruff_time_formatting
AbeCoull Aug 27, 2024
e6fccfd
Update pyproject.toml
AbeCoull Aug 27, 2024
967dea4
change: Add initial output.json
kshyatt-aws Aug 27, 2024
39c790b
add in an empty directory for benchmarking
Aug 27, 2024
228932f
fix: Actually include benchmark script
kshyatt-aws Aug 27, 2024
efa643e
change: Add initial output.json
kshyatt-aws Aug 27, 2024
6a4cb2c
fix: Actually include benchmark script
kshyatt-aws Aug 27, 2024
6aeb9a9
infra: onboard to use ruff
Aug 27, 2024
0567192
remove gitignore after the rebase
Aug 27, 2024
2280398
fix: Don't deploy benchmark results to gh-pages
kshyatt-aws Aug 27, 2024
6003f20
remove github_token
Aug 27, 2024
9a70436
Merge branch 'ksh/speed' into had_a_ruff_time_formatting
AbeCoull Aug 27, 2024
e18ad32
fix: restore GH token
kshyatt-aws Aug 27, 2024
7c24d6e
comment out field dependent on a GH token
Aug 27, 2024
4871f72
Don't publish benchmarking data
AbeCoull Aug 27, 2024
bd3b9e2
Merge branch 'ksh/speed' into had_a_ruff_time_formatting
AbeCoull Aug 27, 2024
c17def4
remove benchmark
Aug 27, 2024
874d8bf
Merge branch 'main' into had_a_ruff_time_formatting
AbeCoull Sep 4, 2024
bd01d2a
fix issues with rebase
Sep 6, 2024
e88a406
fix issues with mainline
Sep 6, 2024
2a6d4a2
linter update
Sep 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
linter update
  • Loading branch information
Coull committed Sep 12, 2024
commit 2a6d4a221a18e199831f70b9bbea23f6bf2ab513
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ optional-dependencies.test = { file = "requirements-test.txt" }

[tool.ruff]
target-version = "py39"
line-length = 120
line-length = 100
format.preview = true
format.docstring-code-line-length = 100
format.docstring-code-format = true
Expand Down
4 changes: 3 additions & 1 deletion src/braket/simulator_v2/base_simulator_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ def reconstruct_complex(v: list | float) -> complex | float:
# Amplitude
if isinstance(result_type.value, dict):
val = task_result.resultTypes[result_ind].value
task_result.resultTypes[result_ind].value = {k: reconstruct_complex(v) for (k, v) in val.items()}
task_result.resultTypes[result_ind].value = {
k: reconstruct_complex(v) for (k, v) in val.items()
}
elif isinstance(result_type.type, StateVector):
val = task_result.resultTypes[result_ind].value
# complex are stored as tuples of reals
Expand Down
3 changes: 2 additions & 1 deletion src/braket/simulator_v2/julia_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def _handle_julia_error(error: str) -> None:
if python_exception is None:
# convert to RuntimeError as JuliaError can't be serialized
py_error = RuntimeError(
"Unable to unwrap internal Julia exception." f"Exception message: {error.exception.message}"
"Unable to unwrap internal Julia exception."
f"Exception message: {error.exception.message}"
)
else:
class_val = getattr(sys.modules["builtins"], str(python_exception))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ def result_types_zero_shots_bell_pair_testing(
run_kwargs: Dict[str, Any],
include_amplitude: bool = True,
):
circuit = Circuit().h(0).cnot(0, 1).expectation(observable=Observable.H() @ Observable.X(), target=[0, 1])
circuit = (
Circuit()
.h(0)
.cnot(0, 1)
.expectation(observable=Observable.H() @ Observable.X(), target=[0, 1])
)
if include_amplitude:
circuit.amplitude(["01", "10", "00", "11"])
if include_state_vector:
Expand All @@ -113,7 +118,9 @@ def result_types_zero_shots_bell_pair_testing(
np.array([1, 0, 0, 1]) / np.sqrt(2),
)
if include_amplitude:
amplitude = result.get_value_by_result_type(ResultType.Amplitude(["01", "10", "00", "11"]))
amplitude = result.get_value_by_result_type(
ResultType.Amplitude(["01", "10", "00", "11"])
)
assert np.isclose(amplitude["01"], 0)
assert np.isclose(amplitude["10"], 0)
assert np.isclose(amplitude["00"], 1 / np.sqrt(2))
Expand Down Expand Up @@ -179,13 +186,18 @@ def result_types_nonzero_shots_bell_pair_testing(device: Device, run_kwargs: Dic
)


def result_types_hermitian_testing(device: Device, run_kwargs: Dict[str, Any], test_program: bool = True):
def result_types_hermitian_testing(
device: Device, run_kwargs: Dict[str, Any], test_program: bool = True
):
shots = run_kwargs["shots"]
theta = 0.543
array = np.array([[1, 2j], [-2j, 0]])

circuit = (
Circuit().rx(0, theta).variance(Observable.Hermitian(array), 0).expectation(Observable.Hermitian(array), 0)
Circuit()
.rx(0, theta)
.variance(Observable.Hermitian(array), 0)
.expectation(Observable.Hermitian(array), 0)
)
if shots:
circuit.add_result_type(ResultType.Sample(Observable.Hermitian(array), 0))
Expand All @@ -196,10 +208,14 @@ def result_types_hermitian_testing(device: Device, run_kwargs: Dict[str, Any], t
expected_mean = 2 * np.sin(theta) + 0.5 * np.cos(theta) + 0.5
expected_var = 0.25 * (np.sin(theta) - 4 * np.cos(theta)) ** 2
expected_eigs = np.linalg.eigvalsh(array)
assert_variance_expectation_sample_result(result, shots, expected_var, expected_mean, expected_eigs)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)


def result_types_all_selected_testing(device: Device, run_kwargs: Dict[str, Any], test_program: bool = True):
def result_types_all_selected_testing(
device: Device, run_kwargs: Dict[str, Any], test_program: bool = True
):
shots = run_kwargs["shots"]
theta = 0.543
array = np.array([[1, 2j], [-2j, 0]])
Expand All @@ -223,7 +239,9 @@ def result_types_all_selected_testing(device: Device, run_kwargs: Dict[str, Any]
var = 0.25 * (np.sin(theta) - 4 * np.cos(theta)) ** 2
expected_var = [var, var]
expected_eigs = np.linalg.eigvalsh(array)
assert_variance_expectation_sample_result(result, shots, expected_var, expected_mean, expected_eigs)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)


def get_result_types_three_qubit_circuit(theta, phi, varphi, obs, obs_targets, shots) -> Circuit:
Expand Down Expand Up @@ -284,7 +302,9 @@ def result_types_tensor_x_y_testing(device: Device, run_kwargs: Dict[str, Any]):
) / 16
expected_eigs = get_pauli_eigenvalues(1)

assert_variance_expectation_sample_result(result, shots, expected_var, expected_mean, expected_eigs)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)


def result_types_tensor_z_z_testing(device: Device, run_kwargs: Dict[str, Any]):
Expand All @@ -303,7 +323,9 @@ def result_types_tensor_z_z_testing(device: Device, run_kwargs: Dict[str, Any]):
expected_var = 0.27801987443788634
expected_eigs = get_pauli_eigenvalues(1)

assert_variance_expectation_sample_result(result, shots, expected_var, expected_mean, expected_eigs)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)


def result_types_tensor_hermitian_hermitian_testing(device: Device, run_kwargs: Dict[str, Any]):
Expand All @@ -329,7 +351,9 @@ def result_types_tensor_hermitian_hermitian_testing(device: Device, run_kwargs:
expected_var = 370.71292282796804
expected_eigs = np.array([-70.90875406, -31.04969387, 0, 3.26468993, 38.693758])

assert_variance_expectation_sample_result(result, shots, expected_var, expected_mean, expected_eigs)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)


def result_types_tensor_z_h_y_testing(device: Device, run_kwargs: Dict[str, Any]):
Expand All @@ -344,15 +368,19 @@ def result_types_tensor_z_h_y_testing(device: Device, run_kwargs: Dict[str, Any]
for task in tasks:
result = device.run(task, **run_kwargs).result()

expected_mean = -(np.cos(varphi) * np.sin(phi) + np.sin(varphi) * np.cos(theta)) / np.sqrt(2)
expected_mean = -(np.cos(varphi) * np.sin(phi) + np.sin(varphi) * np.cos(theta)) / np.sqrt(
2
)
expected_var = (
3
+ np.cos(2 * phi) * np.cos(varphi) ** 2
- np.cos(2 * theta) * np.sin(varphi) ** 2
- 2 * np.cos(theta) * np.sin(phi) * np.sin(2 * varphi)
) / 4
expected_eigs = get_pauli_eigenvalues(1)
assert_variance_expectation_sample_result(result, shots, expected_var, expected_mean, expected_eigs)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)


def result_types_tensor_z_hermitian_testing(device: Device, run_kwargs: Dict[str, Any]):
Expand Down Expand Up @@ -393,15 +421,28 @@ def result_types_tensor_z_hermitian_testing(device: Device, run_kwargs: Dict[str
* (
4
* np.cos(phi)
* (4 + 8 * np.cos(varphi) + np.cos(2 * varphi) - (1 + 6 * np.cos(varphi)) * np.sin(varphi))
* (
4
+ 8 * np.cos(varphi)
+ np.cos(2 * varphi)
- (1 + 6 * np.cos(varphi)) * np.sin(varphi)
)
+ np.sin(phi)
* (15 + 8 * np.cos(varphi) - 11 * np.cos(2 * varphi) + 42 * np.sin(varphi) + 3 * np.sin(2 * varphi))
* (
15
+ 8 * np.cos(varphi)
- 11 * np.cos(2 * varphi)
+ 42 * np.sin(varphi)
+ 3 * np.sin(2 * varphi)
)
)
) / 16

z_array = np.diag([1, -1])
expected_eigs = np.linalg.eigvalsh(np.kron(z_array, array))
assert_variance_expectation_sample_result(result, shots, expected_var, expected_mean, expected_eigs)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)


def result_types_tensor_y_hermitian_testing(device: Device, run_kwargs: Dict[str, Any]):
Expand All @@ -426,7 +467,9 @@ def result_types_tensor_y_hermitian_testing(device: Device, run_kwargs: Dict[str
expected_var = 74.03174647518193
y_array = np.array([[0, -1j], [1j, 0]])
expected_eigs = np.linalg.eigvalsh(np.kron(y_array, array))
assert_variance_expectation_sample_result(result, shots, expected_var, expected_mean, expected_eigs)
assert_variance_expectation_sample_result(
result, shots, expected_var, expected_mean, expected_eigs
)


def result_types_noncommuting_testing(device: Device, run_kwargs: Dict[str, Any]):
Expand Down Expand Up @@ -548,7 +591,9 @@ def noisy_circuit_2qubit_noise_full_probability(device: Device, run_kwargs: Dict
shots = run_kwargs["shots"]
tol = get_tol(shots)
K0 = np.eye(4) * np.sqrt(0.9)
K1 = np.kron(np.array([[0.0, 1.0], [1.0, 0.0]]), np.array([[0.0, 1.0], [1.0, 0.0]])) * np.sqrt(0.1)
K1 = np.kron(np.array([[0.0, 1.0], [1.0, 0.0]]), np.array([[0.0, 1.0], [1.0, 0.0]])) * np.sqrt(
0.1
)
circuit = Circuit().x(0).x(1).kraus((0, 1), [K0, K1]).probability()
tasks = (circuit, circuit.to_ir(ir_type=IRType.OPENQASM))
for task in tasks:
Expand Down Expand Up @@ -597,7 +642,9 @@ def bell_pair_openqasm_testing(device: AwsDevice, run_kwargs: Dict[str, Any]):
no_result_types_testing(program, device, run_kwargs, {"00": 0.5, "11": 0.5})


def openqasm_noisy_circuit_1qubit_noise_full_probability(device: Device, run_kwargs: Dict[str, Any]):
def openqasm_noisy_circuit_1qubit_noise_full_probability(
device: Device, run_kwargs: Dict[str, Any]
):
shots = run_kwargs["shots"]
tol = get_tol(shots)
openqasm_string = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def test_simulator_run_noisy_circuit(noisy_circuit_2_qubit, caplog):

assert all([len(measurement) == 2] for measurement in result.measurements)
assert len(result.measurements) == shots_count
counter = Counter(["".join([str(m) for m in measurement]) for measurement in result.measurements])
counter = Counter([
"".join([str(m) for m in measurement]) for measurement in result.measurements
])
assert counter.keys() == {"10", "11"}
assert 0.0 < counter["10"] / (counter["10"] + counter["11"]) < 0.2
assert 0.8 < counter["11"] / (counter["10"] + counter["11"]) < 1.0
Expand All @@ -97,7 +99,9 @@ def test_simulator_run_bell_pair(bell_ir, caplog):

assert all([len(measurement) == 2] for measurement in result.measurements)
assert len(result.measurements) == shots_count
counter = Counter(["".join([str(m) for m in measurement]) for measurement in result.measurements])
counter = Counter([
"".join([str(m) for m in measurement]) for measurement in result.measurements
])
assert counter.keys() == {"00", "11"}
assert 0.4 < counter["00"] / (counter["00"] + counter["11"]) < 0.6
assert 0.4 < counter["11"] / (counter["00"] + counter["11"]) < 0.6
Expand Down
16 changes: 12 additions & 4 deletions test/unit_tests/braket/simulator_v2/test_pennylane.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@
CIRCUIT_6 = QuantumScript(
ops=[
qml.Hadamard(wires=0),
qml.QubitUnitary(1 / np.sqrt(2) * np.tensor([[1, 1], [1, -1]], requires_grad=True), wires=0),
qml.QubitUnitary(
1 / np.sqrt(2) * np.tensor([[1, 1], [1, -1]], requires_grad=True), wires=0
),
qml.RX(PARAM_6, wires=0),
qml.QubitUnitary(1 / np.sqrt(2) * anp.array([[1, 1], [1, -1]]), wires=0),
qml.CNOT(wires=[0, 1]),
Expand Down Expand Up @@ -156,15 +158,21 @@ def test_local_sim_batch_execute_parallel(mock_run_batch):
)
assert np.allclose(
results[1],
RESULT.get_value_by_result_type(result_types.Expectation(observable=Observable.X(), target=1)),
RESULT.get_value_by_result_type(
result_types.Expectation(observable=Observable.X(), target=1)
),
)
assert np.allclose(
results[2],
RESULT.get_value_by_result_type(result_types.Variance(observable=Observable.Y(), target=2)),
RESULT.get_value_by_result_type(
result_types.Variance(observable=Observable.Y(), target=2)
),
)
assert np.allclose(
results[3],
RESULT.get_value_by_result_type(result_types.Sample(observable=Observable.Z(), target=3)),
RESULT.get_value_by_result_type(
result_types.Sample(observable=Observable.Z(), target=3)
),
)

mock_run_batch.assert_called_with(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ def test_simulator_run_bell_pair(bell_ir, batch_size, caplog):

assert all([len(measurement) == 2] for measurement in result.measurements)
assert len(result.measurements) == shots_count
counter = Counter(["".join([str(m) for m in measurement]) for measurement in result.measurements])
counter = Counter([
"".join([str(m) for m in measurement]) for measurement in result.measurements
])
assert counter.keys() == {"00", "11"}
assert 0.4 < counter["00"] / (counter["00"] + counter["11"]) < 0.6
assert 0.4 < counter["11"] / (counter["00"] + counter["11"]) < 0.6
Expand Down Expand Up @@ -580,7 +582,9 @@ def test_simulator_identity(caplog):
program,
shots=shots_count,
)
counter = Counter(["".join([str(m) for m in measurement]) for measurement in result.measurements])
counter = Counter([
"".join([str(m) for m in measurement]) for measurement in result.measurements
])
assert counter.keys() == {"00"}
assert counter["00"] == shots_count
assert not caplog.text
Expand Down
Loading