| """ |
| Smoke tests for dashboard_core.py — the compute/panel-builder layer behind |
| app_dashboard.py's Quantum Simulator tab. Mirrors the checks performed |
| manually while building the module: correct shapes/keys, no exceptions, |
| every panel builder returns a usable figure (including the empty-data |
| placeholder paths). |
| """ |
|
|
| import json |
|
|
| import matplotlib |
| matplotlib.use("Agg") |
|
|
| import numpy as np |
| import pandas as pd |
| import pytest |
|
|
| import dashboard_core as dc |
| from dashboard_core.qasm_library import _run_on_mps |
| from dashboard_core.simulation_runner import estrai_valore_puro |
|
|
|
|
| BELL_CIRCUIT = "Bell |Φ+⟩" |
| VQE_CIRCUIT = "VQE ansatz Hâ‚‚" |
|
|
|
|
| @pytest.fixture(scope="module") |
| def bell_res(): |
| return dc.run_simulation("Libreria Built-in", BELL_CIRCUIT, "", "ideal", 0.0, 128, 42, use_float32=True) |
|
|
|
|
| @pytest.fixture(scope="module") |
| def noisy_res(): |
| return dc.run_simulation("Libreria Built-in", VQE_CIRCUIT, "", "depolarizing", 0.03, 128, 42, use_float32=True) |
|
|
|
|
| @pytest.fixture(scope="module") |
| def df_vqe_mock(bell_res): |
| return dc.run_vqe_telemetry( |
| bell_res["sim"], bell_res["parser"], dc.QASM_LIBRARY[BELL_CIRCUIT], |
| BELL_CIRCUIT, bell_res["n_qubits"], True, epochs=6, lr=0.05, beta1=0.9, beta2=0.999, seed=42, |
| ) |
|
|
|
|
| @pytest.fixture(scope="module") |
| def df_vqe_real(noisy_res): |
| return dc.run_vqe_telemetry( |
| noisy_res["sim"], noisy_res["parser"], dc.QASM_LIBRARY[VQE_CIRCUIT], |
| VQE_CIRCUIT, noisy_res["n_qubits"], True, epochs=4, lr=0.05, beta1=0.9, beta2=0.999, seed=42, |
| ) |
|
|
|
|
| @pytest.fixture(scope="module") |
| def md_telemetry(): |
| return dc.run_md_telemetry(md_steps=20, md_temp=300) |
|
|
|
|
| |
|
|
| def test_run_simulation_ideal_keys(bell_res): |
| expected_keys = { |
| "prob", "prob_ideal", "noise_factor", "fidelity", "n_qubits", "entropy", |
| "idx_max", "stato_dominante", "tempo", "ram", "nome", "porte_count", |
| "shots_data", "sim", "parser", |
| } |
| assert expected_keys <= set(bell_res.keys()) |
| assert bell_res["n_qubits"] == 2 |
| assert not np.isnan(bell_res["prob"]).any() |
| assert np.isclose(bell_res["prob"].sum(), 1.0, atol=1e-4) |
|
|
|
|
| def test_run_simulation_with_noise(noisy_res): |
| assert not np.isnan(noisy_res["prob"]).any() |
| assert 0.0 <= noisy_res["fidelity"] <= 1.0 + 1e-6 |
|
|
|
|
| def test_run_simulation_noise_is_reproducible_from_seed(): |
| |
| |
| |
| |
| |
| |
| |
| |
| kwargs = dict(source_mode="Libreria Built-in", circuit_name=BELL_CIRCUIT, |
| qasm_text="", noise_model="depolarizing", noise_p=0.2, |
| shots=64, seed=99, use_float32=True) |
| res_a = dc.run_simulation(**kwargs) |
| res_b = dc.run_simulation(**kwargs) |
| np.testing.assert_allclose(res_a["prob"], res_b["prob"]) |
| assert res_a["fidelity"] == pytest.approx(res_b["fidelity"]) |
|
|
|
|
| def test_run_simulation_heavy_circuit_15q(): |
| res = dc.run_simulation("Libreria Built-in", "Error Mitigation (Real-Stress)", "", "bitflip", 0.05, 64, 42, use_float32=True) |
| assert res["n_qubits"] == 15 |
|
|
|
|
| def test_bell_circuit_via_qasm_produces_real_entanglement(bell_res): |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| prob = bell_res["prob"] |
| assert np.isclose(prob[0], 0.5, atol=1e-8) |
| assert np.isclose(prob[3], 0.5, atol=1e-8) |
| assert prob[1] < 1e-8 |
| assert prob[2] < 1e-8 |
|
|
|
|
| |
|
|
| def test_vqe_telemetry_mock_path(df_vqe_mock): |
| assert list(df_vqe_mock.columns) == ["VQE_Energy", "Entropy", "Purity", "Gradient", "Noise_Factor", "Theta_Correction"] |
| assert len(df_vqe_mock) == 6 |
|
|
|
|
| def test_vqe_telemetry_real_path(df_vqe_real): |
| assert list(df_vqe_real.columns) == ["VQE_Energy", "Entropy", "Purity", "Gradient", "Noise_Factor", "Theta_Correction"] |
| assert len(df_vqe_real) == 4 |
|
|
|
|
| def test_vqe_telemetry_heavy_qubit_guard_is_caller_responsibility(): |
| |
| |
| |
| assert dc.QM_MM_HEAVY_QUBIT_THRESHOLD == 12 |
|
|
|
|
| def test_vqe_gradient_matches_finite_difference(): |
| |
| |
| |
| |
| |
| |
| import jax |
| import jax.numpy as jnp |
|
|
| de = __import__("dense_evolution") |
| parser = de.QASMParser() |
| circ = parser.parse(dc.QASM_LIBRARY[VQE_CIRCUIT]) |
| energy_fn, n_params = de.circuit_to_energy_fn(circ, circ.n_qubits) |
| assert n_params > 0 |
|
|
| stato_zero = jnp.zeros(2 ** circ.n_qubits, dtype=jnp.complex128).at[0].set(1.0) |
| rng = np.random.default_rng(7) |
| h_matrix = jnp.diag(jnp.array(np.sort(rng.uniform(-2.5, 2.5, 2 ** circ.n_qubits)))) |
| theta0 = rng.uniform(-np.pi, np.pi, n_params) |
|
|
| energy_and_grad = jax.jit(jax.value_and_grad(energy_fn, argnums=0, has_aux=True)) |
| (_, _), grad = energy_and_grad(jnp.asarray(theta0), h_matrix, stato_zero) |
|
|
| eps = 1e-6 |
| fd_grad = np.zeros(n_params) |
| for i in range(n_params): |
| tp, tm = theta0.copy(), theta0.copy() |
| tp[i] += eps |
| tm[i] -= eps |
| (ep, _), _ = energy_and_grad(jnp.asarray(tp), h_matrix, stato_zero) |
| (em, _), _ = energy_and_grad(jnp.asarray(tm), h_matrix, stato_zero) |
| fd_grad[i] = float((ep - em) / (2 * eps)) |
|
|
| np.testing.assert_allclose(np.asarray(grad), fd_grad, atol=1e-6) |
|
|
|
|
| def test_vqe_energy_trends_downward_over_epochs(): |
| |
| |
| |
| |
| |
| sim = __import__("dense_evolution").DenseSVSimulator(n_qubits=2, use_float32=True) |
| parser = __import__("dense_evolution").QASMParser() |
| np.random.seed(123) |
| df = dc.run_vqe_telemetry( |
| sim, parser, dc.QASM_LIBRARY[VQE_CIRCUIT], VQE_CIRCUIT, 2, True, |
| epochs=40, lr=0.1, beta1=0.9, beta2=0.999, seed=123, |
| ) |
| primi = df["VQE_Energy"].iloc[:5].mean() |
| ultimi = df["VQE_Energy"].iloc[-5:].mean() |
| assert ultimi < primi |
|
|
|
|
| |
|
|
| def test_md_telemetry_shape(md_telemetry): |
| df_md, corr_matrix = md_telemetry |
| assert len(df_md) == 20 |
| assert "Energia_VQE_Ha" in df_md.columns |
| assert corr_matrix.shape == (len(df_md.columns), len(df_md.columns)) |
|
|
|
|
| def test_md_telemetry_mock_mode_labeled_honestly(md_telemetry): |
| |
| |
| |
| df_md, _ = md_telemetry |
| assert df_md.attrs["is_real"] is False |
| assert "dimostrativi" in df_md.attrs["note"].lower() |
|
|
|
|
| def test_md_telemetry_real_mode_shape_and_attrs(bell_res): |
| |
| |
| |
| |
| h2_values = [-1.13, -0.45, 0.12, 0.64] |
| sv = bell_res["sim"].get_statevector() |
| df_md, corr_matrix = dc.run_md_telemetry( |
| md_steps=15, md_temp=300, hamiltonian_values=h2_values, sv=sv, n_qubits=2, seed=42) |
|
|
| assert len(df_md) == 15 |
| assert df_md.attrs["is_real"] is True |
| assert "reale" in df_md.attrs["note"].lower() |
| expected_columns = {"Energia_VQE_Ha", "Entropia_von_Neumann_Bit", |
| "Purita_Stato", "Gradiente_Operatore", "Fattore_Rumore_Termico"} |
| assert expected_columns.issubset(set(df_md.columns)) |
| assert corr_matrix.shape == (len(df_md.columns), len(df_md.columns)) |
|
|
|
|
| def test_md_telemetry_real_mode_physically_sane_values(bell_res): |
| h2_values = [-1.13, -0.45, 0.12, 0.64] |
| sv = bell_res["sim"].get_statevector() |
| df_md, _ = dc.run_md_telemetry( |
| md_steps=10, md_temp=300, hamiltonian_values=h2_values, sv=sv, n_qubits=2, seed=7) |
|
|
| |
| |
| |
| assert (df_md["Purita_Stato"] > 0).all() |
| assert (df_md["Purita_Stato"] <= 1.0 + 1e-9).all() |
| |
| |
| assert (df_md["Entropia_von_Neumann_Bit"] >= -1e-9).all() |
| |
| |
| assert df_md["Energia_VQE_Ha"].between(min(h2_values) - 1e-6, max(h2_values) + 1e-6).all() |
| |
| assert np.isfinite(df_md.to_numpy()).all() |
|
|
|
|
| def test_md_telemetry_real_mode_zero_temperature_skips_noise(bell_res): |
| |
| |
| |
| h2_values = [-1.13, -0.45, 0.12, 0.64] |
| sv = bell_res["sim"].get_statevector() |
| df_md, _ = dc.run_md_telemetry( |
| md_steps=8, md_temp=0, hamiltonian_values=h2_values, sv=sv, n_qubits=2, seed=1) |
| assert df_md.attrs["is_real"] is True |
| assert (df_md["Fattore_Rumore_Termico"] == 0.0).all() |
| |
| |
| assert np.allclose(df_md["Purita_Stato"], 1.0, atol=1e-9) |
|
|
|
|
| def test_md_telemetry_real_mode_reproducible_with_seed(bell_res): |
| h2_values = [-1.13, -0.45, 0.12, 0.64] |
| sv = bell_res["sim"].get_statevector() |
| df_a, _ = dc.run_md_telemetry( |
| md_steps=10, md_temp=300, hamiltonian_values=h2_values, sv=sv, n_qubits=2, seed=99) |
| df_b, _ = dc.run_md_telemetry( |
| md_steps=10, md_temp=300, hamiltonian_values=h2_values, sv=sv, n_qubits=2, seed=99) |
| pd.testing.assert_frame_equal(df_a, df_b) |
|
|
|
|
| def test_md_telemetry_dimension_mismatch_falls_back_to_mock(bell_res): |
| |
| |
| |
| |
| sv = bell_res["sim"].get_statevector() |
| wrong_dim_hamiltonian = [-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5] |
| df_md, _ = dc.run_md_telemetry( |
| md_steps=5, md_temp=300, hamiltonian_values=wrong_dim_hamiltonian, sv=sv, n_qubits=2, seed=0) |
| assert df_md.attrs["is_real"] is False |
| assert "non" in df_md.attrs["note"].lower() and "compatibile" in df_md.attrs["note"].lower() |
|
|
|
|
| |
|
|
| def test_compute_overview_metrics_keys(bell_res): |
| metrics = dc.compute_overview_metrics(bell_res, "ideal", 0.0) |
| expected_labels = { |
| "Qubits", "Hilbert Dim", "Gates", "Entropy", "Concurrence", "Purity", |
| "Spectral σ", "Top State", "P(top)", "RAM", "Time", "Noise", "Noise p", |
| } |
| assert {m["label"] for m in metrics} == expected_labels |
| assert all(isinstance(m["value"], str) for m in metrics) |
| |
| assert all(len(m["value"]) <= 20 for m in metrics) |
|
|
|
|
| def test_compute_overview_metrics_short_values_even_for_heavy_circuit(): |
| |
| |
| res = dc.run_simulation("Libreria Built-in", "Error Mitigation (Real-Stress)", "", "ideal", 0.0, 64, 42, use_float32=True) |
| metrics = dc.compute_overview_metrics(res, "ideal", 0.0) |
| top_state = next(m for m in metrics if m["label"] == "Top State") |
| assert len(top_state["value"]) <= 10 |
| assert "⟩" in top_state["help"] |
|
|
|
|
| |
|
|
| def test_build_panel_overview(bell_res, df_vqe_mock, md_telemetry): |
| df_md, corr_matrix = md_telemetry |
| fig = dc.build_panel_overview(bell_res, df_vqe_mock, corr_matrix, "ideal", 0.0) |
| assert fig is not None |
|
|
|
|
| def test_build_panel_fisica(bell_res): |
| fig = dc.build_panel_fisica(bell_res, seed=42) |
| assert fig is not None |
|
|
|
|
| def test_build_panel_mosaico(bell_res): |
| fig = dc.build_panel_mosaico(bell_res) |
| assert fig is not None |
|
|
|
|
| def test_build_panel_vqe_results_with_data(df_vqe_mock): |
| fig = dc.build_panel_vqe_results(df_vqe_mock) |
| assert fig is not None |
|
|
|
|
| def test_build_panel_vqe_results_empty(): |
| fig = dc.build_panel_vqe_results(pd.DataFrame()) |
| assert fig is not None |
|
|
|
|
| def test_build_panel_md_results_with_data(md_telemetry): |
| df_md, corr_matrix = md_telemetry |
| fig = dc.build_panel_md_results(df_md, corr_matrix) |
| assert fig is not None |
|
|
|
|
| def test_build_panel_md_results_empty(): |
| fig = dc.build_panel_md_results(pd.DataFrame(), pd.DataFrame()) |
| assert fig is not None |
|
|
|
|
| def test_build_panel_performance_with_history(bell_res): |
| history = [{"nome": bell_res["nome"], "n_qubits": bell_res["n_qubits"], |
| "tempo": bell_res["tempo"], "ram": bell_res["ram"]}] |
| fig = dc.build_panel_performance(bell_res, history) |
| assert fig is not None |
|
|
|
|
| def test_build_panel_performance_empty_history(bell_res): |
| fig = dc.build_panel_performance(bell_res, []) |
| assert fig is not None |
|
|
|
|
| def test_build_3d_helix_patch(bell_res): |
| fig = dc.build_3d_helix_patch(bell_res["n_qubits"], bell_res["prob"]) |
| assert fig is not None |
|
|
|
|
| |
|
|
| def test_run_benchmark_scan_small_range(): |
| df = dc.run_benchmark_scan(range(2, 6, 2)) |
| assert list(df.columns) == ["Qubits", "Hilbert_Dim", "Time_s", "RAM_Sim_MB", "Delta_RAM_RSS_MB"] |
| assert len(df) == 2 |
|
|
|
|
| def test_build_provenance_json_roundtrip(bell_res): |
| history = [{"nome": bell_res["nome"], "n_qubits": bell_res["n_qubits"], |
| "tempo": bell_res["tempo"], "ram": bell_res["ram"], |
| "prob_sample": bell_res["prob"]}] |
| payload = dc.build_provenance_json(history) |
| parsed = json.loads(payload) |
| integrity_hash = parsed["metadata"]["integrity_sha256"] |
| assert len(integrity_hash) == 64 and all(c in "0123456789abcdef" for c in integrity_hash) |
| assert len(parsed["records"]) == 1 |
| assert isinstance(parsed["records"][0]["prob_sample"], list) |
|
|
|
|
| |
|
|
| def test_infer_qubit_count_from_qasm(): |
| assert dc.infer_qubit_count_from_qasm(dc.QASM_LIBRARY[VQE_CIRCUIT]) == 2 |
| assert dc.infer_qubit_count_from_qasm(dc.QASM_LIBRARY["Error Mitigation (Real-Stress)"]) == 15 |
| assert dc.infer_qubit_count_from_qasm("") is None |
| assert dc.infer_qubit_count_from_qasm("garbage, no qreg here") is None |
|
|
|
|
| def test_get_compatible_hamiltonians(): |
| compat_2q = dc.get_compatible_hamiltonians(2) |
| assert len(compat_2q) > 0 |
| assert all(len(v) == 4 for v in compat_2q.values()) |
| |
| assert all(v is not None for v in compat_2q.values()) |
| assert dc.get_compatible_hamiltonians(0) == {} |
| assert dc.get_compatible_hamiltonians(None) == {} |
|
|
|
|
| def test_save_custom_hamiltonian_valid_and_duplicate(): |
| library = dict(dc.LIBRERIA_HAMILTONIANE) |
| ok, _ = dc.save_custom_hamiltonian(library, "Test H", "[1.0, -1.0, 0.5, -0.5]") |
| assert ok and "Test H" in library |
| ok2, msg2 = dc.save_custom_hamiltonian(library, "Test H", "[1.0, -1.0, 0.5, -0.5]") |
| assert not ok2 and "esiste già " in msg2 |
|
|
|
|
| def test_save_custom_hamiltonian_invalid_inputs(): |
| library = dict(dc.LIBRERIA_HAMILTONIANE) |
| ok, _ = dc.save_custom_hamiltonian(library, "", "[1.0]") |
| assert not ok |
| ok, _ = dc.save_custom_hamiltonian(library, "Bad JSON", "not json") |
| assert not ok |
| ok, _ = dc.save_custom_hamiltonian(library, "Not a list", '{"a": 1}') |
| assert not ok |
|
|
|
|
| def test_build_panel_hamiltonian_with_and_without_data(): |
| fig = dc.build_panel_hamiltonian([-1.13, -0.45, 0.12, 0.64], "H2 test") |
| assert fig is not None |
| fig_empty = dc.build_panel_hamiltonian(None, "none") |
| assert fig_empty is not None |
|
|
|
|
| def test_vqe_telemetry_with_custom_hamiltonian(noisy_res): |
| compat = dc.get_compatible_hamiltonians(noisy_res["n_qubits"]) |
| values = next(iter(compat.values())) |
| df = dc.run_vqe_telemetry( |
| noisy_res["sim"], noisy_res["parser"], dc.QASM_LIBRARY[VQE_CIRCUIT], |
| VQE_CIRCUIT, noisy_res["n_qubits"], True, epochs=3, lr=0.05, beta1=0.9, beta2=0.999, seed=42, |
| hamiltonian_values=values, |
| ) |
| assert len(df) == 3 |
|
|
|
|
| |
|
|
| def test_on_epoch_callback_mock_path(bell_res): |
| calls = [] |
| df = dc.run_vqe_telemetry( |
| bell_res["sim"], bell_res["parser"], dc.QASM_LIBRARY[BELL_CIRCUIT], |
| BELL_CIRCUIT, bell_res["n_qubits"], True, epochs=5, lr=0.05, beta1=0.9, beta2=0.999, seed=42, |
| on_epoch=lambda e, t, row: calls.append((e, t, row)), |
| ) |
| assert len(calls) == 5 |
| assert calls[0][0] == 0 and calls[0][1] == 5 |
| assert calls[-1][0] == 4 |
| assert set(calls[0][2].keys()) >= {"VQE_Energy", "Entropy", "Purity", "Gradient", "Noise_Factor", "Theta_Correction"} |
| assert len(df) == 5 |
|
|
|
|
| def test_on_epoch_callback_real_path(noisy_res): |
| calls = [] |
| df = dc.run_vqe_telemetry( |
| noisy_res["sim"], noisy_res["parser"], dc.QASM_LIBRARY[VQE_CIRCUIT], |
| VQE_CIRCUIT, noisy_res["n_qubits"], True, epochs=4, lr=0.05, beta1=0.9, beta2=0.999, seed=42, |
| on_epoch=lambda e, t, row: calls.append((e, t, row)), |
| ) |
| assert len(calls) == 4 |
| assert calls[0][1] == 4 |
| assert len(df) == 4 |
|
|
|
|
| def test_run_vqe_telemetry_without_on_epoch_still_works(bell_res): |
| |
| df = dc.run_vqe_telemetry( |
| bell_res["sim"], bell_res["parser"], dc.QASM_LIBRARY[BELL_CIRCUIT], |
| BELL_CIRCUIT, bell_res["n_qubits"], True, epochs=3, lr=0.05, beta1=0.9, beta2=0.999, seed=42, |
| ) |
| assert len(df) == 3 |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| def test_run_on_mps_cx_and_parametric_1q_gates(): |
| |
| |
| mps = _run_on_mps( |
| [["ry", 0, 0.7], ["rz", 1, 0.3], ["u1", 0, 0.1], ["p", 1, 0.2], ["cx", 0, 1]], n_qubits=2) |
| sv = np.asarray(mps.contract_to_statevector()) |
| assert np.isclose(np.linalg.norm(sv), 1.0, atol=1e-6) |
|
|
|
|
| def test_run_on_mps_cz_and_swap_gates(): |
| mps = _run_on_mps([["h", 0, -1], ["cz", 0, 1], ["swap", 1, 2]], n_qubits=3) |
| sv = np.asarray(mps.contract_to_statevector()) |
| assert sv.shape == (8,) |
| assert np.isclose(np.linalg.norm(sv), 1.0, atol=1e-6) |
|
|
|
|
| def test_run_on_mps_cy_gate_matches_dense_simulator(): |
| import dense_evolution as de |
| ops = [["h", 0, -1], ["cy", 0, 1]] |
| mps = _run_on_mps(ops, n_qubits=2) |
| mps_sv = np.asarray(mps.contract_to_statevector()) |
|
|
| dense = de.DenseSVSimulator(n_qubits=2) |
| dense.run_circuit([("h", 0), ("cy", 0, 1)]) |
| dense_sv = np.asarray(dense.get_statevector()) |
| |
| fidelity = abs(np.vdot(mps_sv, dense_sv)) ** 2 |
| assert fidelity == pytest.approx(1.0, abs=1e-6) |
|
|
|
|
| def test_run_on_mps_cp_and_crz_parametric_2q_gates(): |
| mps = _run_on_mps( |
| [["h", 0, -1], ["h", 1, -1], ["cp", 0, 1, 0.5], ["crz", 1, 2, 0.3]], n_qubits=3) |
| sv = np.asarray(mps.contract_to_statevector()) |
| assert np.isclose(np.linalg.norm(sv), 1.0, atol=1e-6) |
|
|
|
|
| def test_run_on_mps_ccx_gate(): |
| mps = _run_on_mps([["h", 0, -1], ["h", 1, -1], ["ccx", 0, 1, 2]], n_qubits=3) |
| sv = np.asarray(mps.contract_to_statevector()) |
| assert np.isclose(np.linalg.norm(sv), 1.0, atol=1e-6) |
|
|
|
|
| def test_run_on_mps_u2_u3_skipped_with_warning(capsys): |
| |
| |
| mps = _run_on_mps([["h", 0, -1], ["u2", 0, 0.1], ["u3", 0, 0.2]], n_qubits=1) |
| sv = np.asarray(mps.contract_to_statevector()) |
| assert np.isclose(np.linalg.norm(sv), 1.0, atol=1e-6) |
| captured = capsys.readouterr() |
| assert "u2" in captured.out and "u3" in captured.out |
| assert "Warning" in captured.out |
|
|
|
|
| def test_run_on_mps_unhandled_gate_name_skipped_with_warning(capsys): |
| |
| |
| |
| |
| mps = _run_on_mps([["h", 0, -1], ["ch", 0, 1]], n_qubits=2) |
| sv = np.asarray(mps.contract_to_statevector()) |
| assert np.isclose(np.linalg.norm(sv), 1.0, atol=1e-6) |
| captured = capsys.readouterr() |
| assert "ch" in captured.out and "Warning" in captured.out |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| def test_estrai_valore_puro_none_returns_zero(): |
| assert estrai_valore_puro(None) == 0 |
|
|
|
|
| def test_estrai_valore_puro_calls_function_like_callable(): |
| assert estrai_valore_puro(lambda: 5) == 5 |
|
|
|
|
| def test_estrai_valore_puro_function_like_callable_exception_returns_zero(): |
| def boom(): |
| raise RuntimeError("nope") |
| assert estrai_valore_puro(boom) == 0 |
|
|
|
|
| def test_estrai_valore_puro_calls_generic_callable_object(): |
| class Foo: |
| def __call__(self): |
| return 42 |
| assert estrai_valore_puro(Foo()) == 42 |
|
|
|
|
| def test_estrai_valore_puro_generic_callable_exception_falls_through(): |
| class Boom: |
| def __call__(self): |
| raise RuntimeError("nope") |
| |
| |
| b = Boom() |
| assert estrai_valore_puro(b) is b |
|
|
|
|
| def test_estrai_valore_puro_index_attribute_plain_value(): |
| class QubitRef: |
| def __init__(self, idx): |
| self.index = idx |
| assert estrai_valore_puro(QubitRef(3)) == 3 |
|
|
|
|
| def test_estrai_valore_puro_index_attribute_callable_value(): |
| class QubitRef: |
| def __init__(self, idx): |
| self.index = lambda: idx |
| assert estrai_valore_puro(QubitRef(7)) == 7 |
|
|
|
|
| def test_estrai_valore_puro_value_attribute_plain_value(): |
| class ParamRef: |
| def __init__(self, v): |
| self.value = v |
| assert estrai_valore_puro(ParamRef(0.5)) == 0.5 |
|
|
|
|
| def test_estrai_valore_puro_value_attribute_callable_value(): |
| class ParamRef: |
| def __init__(self, v): |
| self.value = lambda: v |
| assert estrai_valore_puro(ParamRef(0.25)) == 0.25 |
|
|
|
|
| def test_estrai_valore_puro_string_with_dot_becomes_float(): |
| assert estrai_valore_puro("3.5") == 3.5 |
|
|
|
|
| def test_estrai_valore_puro_string_digits_become_int(): |
| assert estrai_valore_puro("42") == 42 |
|
|
|
|
| def test_estrai_valore_puro_non_numeric_string_returned_as_is(): |
| assert estrai_valore_puro("abc") == "abc" |
| assert estrai_valore_puro("a.b") == "a.b" |
|
|
|
|
| def test_estrai_valore_puro_plain_object_returned_unchanged(): |
| class Opaque: |
| pass |
| o = Opaque() |
| assert estrai_valore_puro(o) is o |
|
|
|
|
| def test_estrai_valore_puro_numpy_scalar_types_pass_through(): |
| assert estrai_valore_puro(np.int64(9)) == 9 |
| assert estrai_valore_puro(np.float32(1.5)) == 1.5 |
|
|
|
|
| |
|
|
| def test_run_simulation_ccx_gate_through_full_pipeline(): |
| |
| |
| |
| |
| |
| res = dc.run_simulation("Libreria Built-in", "Grover_3q_Oracle_111", "", "ideal", 0.0, 64, 42, use_float32=True) |
| assert res["n_qubits"] == 3 |
| assert np.isclose(np.sum(res["prob"]), 1.0, atol=1e-5) |
|
|
|
|
| def test_run_simulation_cp_gate_through_full_pipeline(): |
| |
| |
| res = dc.run_simulation("Libreria Built-in", "QFT 4 qubit", "", "ideal", 0.0, 64, 42, use_float32=True) |
| assert res["n_qubits"] == 4 |
| assert np.isclose(np.sum(res["prob"]), 1.0, atol=1e-5) |
|
|
|
|
| def test_run_simulation_mps_engine_with_noise(): |
| |
| |
| |
| |
| |
| res = dc.run_simulation( |
| "Libreria Built-in", BELL_CIRCUIT, "", "depolarizing", 0.05, 64, 42, |
| use_float32=True, engine="mps") |
| assert res["n_qubits"] == 2 |
| assert 0.0 <= res["fidelity"] <= 1.0 + 1e-6 |
| assert np.isclose(np.sum(res["prob"]), 1.0, atol=1e-5) |
|
|
|
|
| def test_run_simulation_custom_qasm_text_mode(): |
| |
| |
| |
| |
| custom_qasm = 'OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; creg c[2]; h q[0]; cx q[0],q[1]; measure q -> c;' |
| res = dc.run_simulation("Custom Workspace", "", custom_qasm, "ideal", 0.0, 64, 42, use_float32=True) |
| assert res["nome"] == "Custom Workspace" |
| assert res["n_qubits"] == 2 |
| assert np.isclose(np.sum(res["prob"]), 1.0, atol=1e-5) |
|
|
|
|
| def test_run_simulation_mps_engine_rejects_over_24_qubits(): |
| |
| |
| |
| |
| |
| big_qasm = 'OPENQASM 2.0; include "qelib1.inc"; qreg q[30]; creg c[30];' |
| with pytest.raises(ValueError, match="24 qubit"): |
| dc.run_simulation("Custom Workspace", "", big_qasm, "ideal", 0.0, 64, 42, use_float32=True, engine="mps") |
|
|
|
|
| def test_run_simulation_chunk_engine(monkeypatch): |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import dense_evolution.chunk as chunk_module |
| monkeypatch.setattr(chunk_module, "get_dynamic_chunk", lambda dtype_target: 1) |
|
|
| res = dc.run_simulation("Libreria Built-in", BELL_CIRCUIT, "", "ideal", 0.0, 64, 42, use_float32=True) |
| from dense_evolution import Chunk |
| assert isinstance(res["sim"], Chunk) |
| assert np.isclose(np.sum(res["prob"]), 1.0, atol=1e-5) |
|
|