From 7aaa3f5c523893cb2d92fdad42bf4ec37ff86594 Mon Sep 17 00:00:00 2001 From: Andrew Meyer Date: Thu, 17 Jun 2021 10:36:04 -0700 Subject: [PATCH] Update for pyQuil v3 --- .gitlab-ci.yml | 2 +- CHANGELOG.md | 4 + docs/examples/chip_scan.ipynb | 22 ++-- .../examples/direct_fidelity_estimation.ipynb | 8 +- docs/examples/entangled_states.ipynb | 11 +- docs/examples/graph-state.json | 1 + docs/examples/hinton_plots.ipynb | 2 +- docs/examples/quantum_volume.ipynb | 5 +- .../qubit_spectroscopy_cz_ramsey.ipynb | 6 +- docs/examples/randomized_benchmarking.ipynb | 7 +- .../randomized_benchmarking_interleaved.ipynb | 6 +- .../randomized_benchmarking_unitarity.ipynb | 6 +- docs/examples/robust_phase_estimation.ipynb | 12 +- docs/examples/state_and_process_plots.ipynb | 2 +- docs/examples/superoperator_tools.ipynb | 2 +- docs/examples/tomography_process.ipynb | 6 +- docs/examples/tomography_state.ipynb | 4 +- .../classical_logic/ripple_carry_adder.py | 19 ++-- forest/benchmarking/quantum_volume.py | 15 +-- forest/benchmarking/readout.py | 16 +-- forest/benchmarking/tests/conftest.py | 105 ++++-------------- .../benchmarking/tests/test_calculational.py | 11 +- .../tests/test_classical_logic_primites.py | 10 +- .../tests/test_compose_superoperators.py | 7 +- .../tests/test_direct_fidelity_estimation.py | 2 +- .../tests/test_distance_measures.py | 10 +- .../tests/test_observable_estimation.py | 102 ++++++++--------- .../tests/test_process_tomography.py | 4 +- .../tests/test_project_superoperators.py | 7 +- .../benchmarking/tests/test_quantum_volume.py | 4 +- forest/benchmarking/tests/test_readout.py | 5 +- .../tests/test_ripple_carry_adder.py | 6 +- .../tests/test_state_tomography.py | 2 +- .../test_superoperator_transformations.py | 4 +- .../tests/test_validate_operators.py | 2 +- .../tests/test_validate_superoperator.py | 2 +- forest/benchmarking/tomography.py | 2 +- forest/benchmarking/utils.py | 14 +-- requirements.txt | 4 +- 39 files changed, 199 insertions(+), 260 deletions(-) create mode 100644 docs/examples/graph-state.json diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 30dece68..3faf8d86 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,4 @@ -image: python:3.6 +image: python:3.7 services: - name: rigetti/qvm diff --git a/CHANGELOG.md b/CHANGELOG.md index cc32c8b1..86a9b483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Changelog [v0.7.2](https://github.com/rigetti/forest-benchmarking/compare/v0.7.1...master) (in development) ------------------------------------------------------------------------------------ +### Announcements + +- Upgraded pyQuil to v3 (gh-215). As a result, the minimum supported Python version is now 3.7. + ### Bugfixes - Fix compiler timeout during test (@jlapeyre) (gh-208) diff --git a/docs/examples/chip_scan.ipynb b/docs/examples/chip_scan.ipynb index ec9dd3c5..f46acad7 100644 --- a/docs/examples/chip_scan.ipynb +++ b/docs/examples/chip_scan.ipynb @@ -16,7 +16,6 @@ "import numpy as np\n", "from numpy import pi\n", "from pyquil.api import get_qc\n", - "from pyquil.api._devices import get_lattice\n", "from pyquil import Program\n", "from pyquil.gates import *\n", "\n", @@ -38,11 +37,8 @@ "metadata": {}, "outputs": [], "source": [ - "# lattice_name = 'Aspen-1-5Q-B'\n", - "lattice_name = '9q-square-noisy-qvm'\n", - "# lattice = get_lattice(lattice_name)\n", - "# stored_specs = lattice.get_specs()\n", - "# print(stored_specs)" + "# quantum_processor_id = 'Aspen-1-5Q-B'\n", + "quantum_processor_id = '9q-square-noisy-qvm'" ] }, { @@ -97,7 +93,7 @@ "source": [ "import networkx as nx\n", "\n", - "qc = get_qc(lattice_name, as_qvm=True, noisy=True)\n", + "qc = get_qc(quantum_processor_id, as_qvm=True, noisy=True)\n", "qubits = qc.qubits()\n", "print(qubits)\n", "graph = qc.qubit_topology()\n", @@ -228,7 +224,7 @@ "source": [ "print('Single qubit confusion matrices: \\n', single_qubit_cms)\n", "print('Avg. fidelity per qubit: ', [np.round(np.trace(cm)/2, 3) for cm in single_qubit_cms.values()])\n", - "from pyquil.gate_matrices import Z as Z_mat\n", + "from pyquil.simulation.matrices import Z as Z_mat\n", "print('Asymmetry magnitude: ', [np.round(np.trace(Z_mat @ cm)/2, 3) for cm in single_qubit_cms.values()])" ] }, @@ -391,9 +387,9 @@ "metadata": {}, "outputs": [], "source": [ - "from pyquil.api import get_benchmarker\n", + "from pyquil.api import BenchmarkConnection\n", "from forest.benchmarking.randomized_benchmarking import do_rb\n", - "bm = get_benchmarker()" + "bm = BenchmarkConnection()" ] }, { @@ -842,8 +838,8 @@ "source": [ "from forest.benchmarking.direct_fidelity_estimation import do_dfe\n", "\n", - "from pyquil.api import get_benchmarker\n", - "bm = get_benchmarker()\n", + "from pyquil.api import BenchmarkConnection\n", + "bm = BenchmarkConnection()\n", "\n", "cz_fidelities = {}\n", "for edge in graph.edges():\n", @@ -1032,4 +1028,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/docs/examples/direct_fidelity_estimation.ipynb b/docs/examples/direct_fidelity_estimation.ipynb index ff4bb4ba..3e9abade 100644 --- a/docs/examples/direct_fidelity_estimation.ipynb +++ b/docs/examples/direct_fidelity_estimation.ipynb @@ -154,7 +154,7 @@ "from pyquil.paulis import ID\n", "from pyquil.gates import I, X, MEASURE, H, CNOT, RY, CZ\n", "from pyquil import Program, get_qc\n", - "from pyquil.api import get_benchmarker\n", + "from pyquil.api import BenchmarkConnection\n", "from forest.benchmarking.direct_fidelity_estimation import ( generate_exhaustive_state_dfe_experiment, \n", " generate_exhaustive_process_dfe_experiment,\n", " generate_monte_carlo_state_dfe_experiment, \n", @@ -178,7 +178,7 @@ "# noisy QVM\n", "noisy_qvm = get_qc(\"9q-generic-qvm\", as_qvm=True, noisy=True)\n", "\n", - "bm = get_benchmarker()" + "bm = BenchmarkConnection()" ] }, { @@ -275,7 +275,7 @@ } ], "source": [ - "from pyquil.gate_matrices import I as Imatrix, H as Hmatrix, CNOT as CNOTmatrix\n", + "from pyquil.simulation.matrices import I as Imatrix, H as Hmatrix, CNOT as CNOTmatrix\n", "\n", "U_ideal = CNOTmatrix @ np.kron(Hmatrix, Imatrix)\n", "print(U_ideal)\n", @@ -1491,4 +1491,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} +} \ No newline at end of file diff --git a/docs/examples/entangled_states.ipynb b/docs/examples/entangled_states.ipynb index 4678f8fc..36ddfd51 100644 --- a/docs/examples/entangled_states.ipynb +++ b/docs/examples/entangled_states.ipynb @@ -90,10 +90,9 @@ " program.wrap_in_numshots_loop(n_shots)\n", " executable = qc.compile(program)\n", "\n", - " qc.qam.load(executable)\n", " for theta in np.linspace(-np.pi, np.pi, 21):\n", - " qc.qam.write_memory(region_name='theta', value=theta)\n", - " bitstrings = qc.qam.run().wait().read_from_memory_region(region_name='ro')\n", + " executable.write_memory(region_name='theta', value=theta)\n", + " bitstrings = qc.qam.run(executable).readout_data.get('ro')\n", " parities = np.sum(bitstrings, axis=1) % 2\n", " avg_parity = np.mean(parities)\n", " results.append({\n", @@ -438,7 +437,7 @@ "metadata": {}, "outputs": [], "source": [ - "from pyquil.api import get_benchmarker\n", + "from pyquil.api import BenchmarkConnection\n", "from forest.benchmarking.direct_fidelity_estimation import ( generate_exhaustive_state_dfe_experiment, \n", " acquire_dfe_data,\n", " estimate_dfe )" @@ -450,7 +449,7 @@ "metadata": {}, "outputs": [], "source": [ - "bm = get_benchmarker()" + "bm = BenchmarkConnection()" ] }, { @@ -530,4 +529,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/docs/examples/graph-state.json b/docs/examples/graph-state.json new file mode 100644 index 00000000..62274b46 --- /dev/null +++ b/docs/examples/graph-state.json @@ -0,0 +1 @@ +{"focal_node":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1,"13":1,"14":1,"15":1,"16":1,"17":1,"18":1,"19":1,"20":1,"21":2,"22":2,"23":2,"24":2,"25":2,"26":2,"27":2,"28":2,"29":2,"30":2,"31":2,"32":2,"33":2,"34":2,"35":2,"36":2,"37":2,"38":2,"39":2,"40":2,"41":2,"42":3,"43":3,"44":3,"45":3,"46":3,"47":3,"48":3,"49":3,"50":3,"51":3,"52":3,"53":3,"54":3,"55":3,"56":3,"57":3,"58":3,"59":3,"60":3,"61":3,"62":3},"theta":{"0":-3.1415926536,"1":-2.8274333882,"2":-2.5132741229,"3":-2.1991148575,"4":-1.8849555922,"5":-1.5707963268,"6":-1.2566370614,"7":-0.9424777961,"8":-0.6283185307,"9":-0.3141592654,"10":0.0,"11":0.3141592654,"12":0.6283185307,"13":0.9424777961,"14":1.2566370614,"15":1.5707963268,"16":1.8849555922,"17":2.1991148575,"18":2.5132741229,"19":2.8274333882,"20":3.1415926536,"21":-3.1415926536,"22":-2.8274333882,"23":-2.5132741229,"24":-2.1991148575,"25":-1.8849555922,"26":-1.5707963268,"27":-1.2566370614,"28":-0.9424777961,"29":-0.6283185307,"30":-0.3141592654,"31":0.0,"32":0.3141592654,"33":0.6283185307,"34":0.9424777961,"35":1.2566370614,"36":1.5707963268,"37":1.8849555922,"38":2.1991148575,"39":2.5132741229,"40":2.8274333882,"41":3.1415926536,"42":-3.1415926536,"43":-2.8274333882,"44":-2.5132741229,"45":-2.1991148575,"46":-1.8849555922,"47":-1.5707963268,"48":-1.2566370614,"49":-0.9424777961,"50":-0.6283185307,"51":-0.3141592654,"52":0.0,"53":0.3141592654,"54":0.6283185307,"55":0.9424777961,"56":1.2566370614,"57":1.5707963268,"58":1.8849555922,"59":2.1991148575,"60":2.5132741229,"61":2.8274333882,"62":3.1415926536},"n_bitstrings":{"0":1000,"1":1000,"2":1000,"3":1000,"4":1000,"5":1000,"6":1000,"7":1000,"8":1000,"9":1000,"10":1000,"11":1000,"12":1000,"13":1000,"14":1000,"15":1000,"16":1000,"17":1000,"18":1000,"19":1000,"20":1000,"21":1000,"22":1000,"23":1000,"24":1000,"25":1000,"26":1000,"27":1000,"28":1000,"29":1000,"30":1000,"31":1000,"32":1000,"33":1000,"34":1000,"35":1000,"36":1000,"37":1000,"38":1000,"39":1000,"40":1000,"41":1000,"42":1000,"43":1000,"44":1000,"45":1000,"46":1000,"47":1000,"48":1000,"49":1000,"50":1000,"51":1000,"52":1000,"53":1000,"54":1000,"55":1000,"56":1000,"57":1000,"58":1000,"59":1000,"60":1000,"61":1000,"62":1000},"avg_parity":{"0":0.531,"1":0.34,"2":0.212,"3":0.097,"4":0.032,"5":0.0,"6":0.015,"7":0.098,"8":0.217,"9":0.352,"10":0.523,"11":0.644,"12":0.795,"13":0.904,"14":0.976,"15":1.0,"16":0.973,"17":0.896,"18":0.809,"19":0.655,"20":0.48,"21":0.469,"22":0.314,"23":0.185,"24":0.097,"25":0.032,"26":0.0,"27":0.035,"28":0.106,"29":0.22,"30":0.354,"31":0.492,"32":0.658,"33":0.796,"34":0.893,"35":0.976,"36":1.0,"37":0.969,"38":0.889,"39":0.784,"40":0.679,"41":0.491,"42":0.507,"43":0.336,"44":0.203,"45":0.082,"46":0.036,"47":0.0,"48":0.024,"49":0.105,"50":0.204,"51":0.341,"52":0.479,"53":0.634,"54":0.78,"55":0.906,"56":0.969,"57":1.0,"58":0.973,"59":0.904,"60":0.797,"61":0.653,"62":0.491}} \ No newline at end of file diff --git a/docs/examples/hinton_plots.ipynb b/docs/examples/hinton_plots.ipynb index a3f88e72..8a26f13b 100644 --- a/docs/examples/hinton_plots.ipynb +++ b/docs/examples/hinton_plots.ipynb @@ -22,7 +22,7 @@ "outputs": [], "source": [ "import numpy as np\n", - "from pyquil.gate_matrices import X, Y, Z\n", + "from pyquil.simulation.matrices import X, Y, Z\n", "PROJ_ZERO = np.array([[1, 0], [0, 0]])\n", "PROJ_ONE = np.array([[0, 0], [0, 1]])\n", "ID = PROJ_ZERO + PROJ_ONE\n", diff --git a/docs/examples/quantum_volume.ipynb b/docs/examples/quantum_volume.ipynb index 1d23d357..303d332e 100644 --- a/docs/examples/quantum_volume.ipynb +++ b/docs/examples/quantum_volume.ipynb @@ -433,8 +433,9 @@ "metadata": {}, "outputs": [], "source": [ + "from pyquil import Program\n", "from forest.benchmarking.quantum_volume import generate_abstract_qv_circuit, _naive_program_generator, collect_heavy_outputs\n", - "from pyquil.numpy_simulator import NumpyWavefunctionSimulator\n", + "from pyquil.simulation import NumpyWavefunctionSimulator\n", "from pyquil.gates import RESET\n", "import time\n", "\n", @@ -739,4 +740,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} +} \ No newline at end of file diff --git a/docs/examples/qubit_spectroscopy_cz_ramsey.ipynb b/docs/examples/qubit_spectroscopy_cz_ramsey.ipynb index f6d47b73..8696acaa 100644 --- a/docs/examples/qubit_spectroscopy_cz_ramsey.ipynb +++ b/docs/examples/qubit_spectroscopy_cz_ramsey.ipynb @@ -73,8 +73,8 @@ "metadata": {}, "outputs": [], "source": [ - "# if you are on the QPU you can look at `qc.device.specs`\n", - "# qc.device.specs" + "# if you are on the QPU you can look at the ISA\n", + "# qc.quantum_processor.to_compiler_isa()" ] }, { @@ -202,4 +202,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/docs/examples/randomized_benchmarking.ipynb b/docs/examples/randomized_benchmarking.ipynb index 0a3d0d04..502b62da 100644 --- a/docs/examples/randomized_benchmarking.ipynb +++ b/docs/examples/randomized_benchmarking.ipynb @@ -142,7 +142,6 @@ "\n", "import numpy as np\n", "\n", - "from pyquil.api import get_benchmarker\n", "from forest.benchmarking.randomized_benchmarking import (generate_rb_sequence,\n", " generate_rb_experiments, acquire_rb_data,\n", " get_stats_by_qubit_group, fit_rb_results)\n", @@ -156,9 +155,9 @@ "metadata": {}, "outputs": [], "source": [ - "from pyquil.api import get_qc, get_benchmarker\n", + "from pyquil.api import get_qc, BenchmarkConnection\n", "qc = get_qc(\"9q-square-noisy-qvm\")\n", - "bm = get_benchmarker()" + "bm = BenchmarkConnection()" ] }, { @@ -671,4 +670,4 @@ }, "nbformat": 4, "nbformat_minor": 4 -} +} \ No newline at end of file diff --git a/docs/examples/randomized_benchmarking_interleaved.ipynb b/docs/examples/randomized_benchmarking_interleaved.ipynb index 2d1d78a9..77c4a8c8 100644 --- a/docs/examples/randomized_benchmarking_interleaved.ipynb +++ b/docs/examples/randomized_benchmarking_interleaved.ipynb @@ -56,7 +56,7 @@ "\n", "import numpy as np\n", "\n", - "from pyquil.api import get_benchmarker, get_qc\n", + "from pyquil.api import BenchmarkConnection, get_qc\n", "from forest.benchmarking.randomized_benchmarking import *\n", "from forest.benchmarking.plotting import plot_figure_for_fit\n", "\n", @@ -80,7 +80,7 @@ "metadata": {}, "outputs": [], "source": [ - "bm = get_benchmarker()\n", + "bm = BenchmarkConnection()\n", "qc = get_qc(\"9q-square-noisy-qvm\", noisy=True)" ] }, @@ -422,4 +422,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/docs/examples/randomized_benchmarking_unitarity.ipynb b/docs/examples/randomized_benchmarking_unitarity.ipynb index 3d1e1afa..a645686f 100644 --- a/docs/examples/randomized_benchmarking_unitarity.ipynb +++ b/docs/examples/randomized_benchmarking_unitarity.ipynb @@ -88,11 +88,11 @@ "from forest.benchmarking.plotting import plot_figure_for_fit\n", "from forest.benchmarking.randomized_benchmarking import *\n", "\n", - "from pyquil.api import get_benchmarker\n", + "from pyquil.api import BenchmarkConnection\n", "from pyquil import Program, get_qc\n", "\n", "noisy_qc = get_qc('2q-qvm', noisy=True)\n", - "bm = get_benchmarker()" + "bm = BenchmarkConnection()" ] }, { @@ -357,4 +357,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/docs/examples/robust_phase_estimation.ipynb b/docs/examples/robust_phase_estimation.ipynb index 249ed857..908166fb 100644 --- a/docs/examples/robust_phase_estimation.ipynb +++ b/docs/examples/robust_phase_estimation.ipynb @@ -504,7 +504,7 @@ "source": [ "from pyquil import Program\n", "from pyquil.quil import DefGate, Gate\n", - "from pyquil.gate_matrices import X as x_mat, Y as y_mat, Z as z_mat\n", + "from pyquil.simulation.matrices import X as x_mat, Y as y_mat, Z as z_mat\n", "\"\"\"\n", "Procedure and notation follows Sections III A, B, and C in \n", "[RPE] Robust Calibration of a Universal Single-Qubit Gate-Set via Robust Phase Estimation\n", @@ -661,12 +661,12 @@ } ], "source": [ - "from pyquil.device import gates_in_isa\n", - "from pyquil.noise import decoherence_noise_with_asymmetric_ro, _decoherence_noise_model\n", - "# noise_model = decoherence_noise_with_asymmetric_ro(gates=gates_in_isa(qc.device.get_isa()), p00=0.92, p11=.87)\n", + "from pyquil.noise import decoherence_noise_with_asymmetric_ro, _decoherence_noise_model, _get_qvm_noise_supported_gates\n", + "# noise_model = decoherence_noise_with_asymmetric_ro(qc.device.get_isa(), p00=0.92, p11=.87)\n", "T1=20e-6\n", "T2=10e-6\n", - "noise_model = _decoherence_noise_model(gates=gates_in_isa(qc.device.get_isa()), T1=T1, T2=T2, ro_fidelity=1.)\n", + "gates = _get_qvm_noise_supported_gates(qc.device.to_compiler_isa())\n", + "noise_model = _decoherence_noise_model(gates=gates, T1=T1, T2=T2, ro_fidelity=1.)\n", "\n", "qc = get_qc(\"9q-square\", as_qvm=True, noisy=False)\n", "qc.qam.noise_model = noise_model\n", @@ -705,4 +705,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/docs/examples/state_and_process_plots.ipynb b/docs/examples/state_and_process_plots.ipynb index 2539a43a..4ccc68c8 100644 --- a/docs/examples/state_and_process_plots.ipynb +++ b/docs/examples/state_and_process_plots.ipynb @@ -18,7 +18,7 @@ "from matplotlib import pyplot as plt\n", "\n", "# quantum related things\n", - "from pyquil.gate_matrices import X, Y, Z, H, CNOT, CZ\n", + "from pyquil.simulation.matrices import X, Y, Z, H, CNOT, CZ\n", "from forest.benchmarking.operator_tools.superoperator_transformations import *\n", "from forest.benchmarking.utils import n_qubit_pauli_basis\n" ] diff --git a/docs/examples/superoperator_tools.ipynb b/docs/examples/superoperator_tools.ipynb index 4b91b3ed..afe8f743 100644 --- a/docs/examples/superoperator_tools.ipynb +++ b/docs/examples/superoperator_tools.ipynb @@ -92,7 +92,7 @@ "outputs": [], "source": [ "import numpy as np\n", - "from pyquil.gate_matrices import I, X, Y, Z, H, CNOT" + "from pyquil.simulation.matrices import I, X, Y, Z, H, CNOT" ] }, { diff --git a/docs/examples/tomography_process.ipynb b/docs/examples/tomography_process.ipynb index e0c2d1ba..3b5926aa 100644 --- a/docs/examples/tomography_process.ipynb +++ b/docs/examples/tomography_process.ipynb @@ -255,7 +255,7 @@ "source": [ "# numerical representation of the true process\n", "\n", - "from pyquil.gate_matrices import RX as RX_matrix\n", + "from pyquil.simulation.matrices import RX as RX_matrix\n", "from forest.benchmarking.operator_tools import kraus2choi\n", "\n", "kraus_true = RX_matrix(np.pi)\n", @@ -264,7 +264,7 @@ "choi_true = kraus2choi(kraus_true)\n", "print('The Choi representation is:\\n', np.real_if_close(np.round(choi_true, 2)))\n", "\n", - "from pyquil.gate_matrices import X as X_matrix\n", + "from pyquil.simulation.matrices import X as X_matrix\n", "choi_x_gate = kraus2choi(X_matrix)\n", "print('\\n The X gate choi matrix is:\\n', np.real_if_close(np.round(choi_x_gate)))" ] @@ -761,7 +761,7 @@ } ], "source": [ - "from pyquil.gate_matrices import CNOT as CNOT_matrix\n", + "from pyquil.simulation.matrices import CNOT as CNOT_matrix\n", "process_choi_ideal = kraus2choi(CNOT_matrix)\n", "_print_big_matrix(process_choi_ideal)" ] diff --git a/docs/examples/tomography_state.ipynb b/docs/examples/tomography_state.ipynb index 7ef9690a..27c57225 100644 --- a/docs/examples/tomography_state.ipynb +++ b/docs/examples/tomography_state.ipynb @@ -1081,7 +1081,7 @@ "outputs": [], "source": [ "from pyquil.pyqvm import PyQVM\n", - "from pyquil.reference_simulator import ReferenceDensitySimulator\n", + "from pyquil.simulation import ReferenceDensitySimulator\n", "mixed_qvm = get_qc('1q-pyqvm')\n", "# replace the simulator with a ReferenceDensitySimulator for mixed states\n", "mixed_qvm.qam.wf_simulator = ReferenceDensitySimulator(n_qubits=1, rs=mixed_qvm.qam.rs)" @@ -1209,4 +1209,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/forest/benchmarking/classical_logic/ripple_carry_adder.py b/forest/benchmarking/classical_logic/ripple_carry_adder.py index df0371dd..75acf5d1 100644 --- a/forest/benchmarking/classical_logic/ripple_carry_adder.py +++ b/forest/benchmarking/classical_logic/ripple_carry_adder.py @@ -17,17 +17,16 @@ https://arxiv.org/abs/quant-ph/9511018 """ from typing import Sequence, Tuple, Optional + import networkx as nx import numpy as np +from pyquil.api import QuantumComputer +from pyquil.gates import MEASURE, RESET +from pyquil.quil import Pragma +from pyquil.simulation.tools import all_bitstrings from scipy.spatial.distance import hamming from tqdm import tqdm -from pyquil.gates import CNOT, CCNOT, X, I, H, CZ, MEASURE, RESET -from pyquil import Program -from pyquil.quil import Pragma -from pyquil.api import QuantumComputer -from pyquil.unitary_tools import all_bitstrings - from forest.benchmarking.classical_logic.primitives import * from forest.benchmarking.utils import bit_array_to_int, int_to_bit_array, bitstring_prep, \ parameterized_bitstring_prep @@ -304,11 +303,11 @@ def get_n_bit_adder_results(qc: QuantumComputer, n_bits: int, nat_quil = qc.compiler.quil_to_native_quil(prog) exe = qc.compiler.native_quil_to_executable(nat_quil) - # Run it on the QPU or QVM if use_param_program: - results = qc.run(exe, memory_map={REG_NAME: bits}) - else: - results = qc.run(exe) + exe.write_memory(region_name=REG_NAME, value=bits) + + # Run it on the QPU or QVM + results = qc.run(exe).readout_data.get('ro') all_results.append(results) return all_results diff --git a/forest/benchmarking/quantum_volume.py b/forest/benchmarking/quantum_volume.py index fc128407..da4775a2 100644 --- a/forest/benchmarking/quantum_volume.py +++ b/forest/benchmarking/quantum_volume.py @@ -6,10 +6,11 @@ from copy import copy from pyquil.api import QuantumComputer -from pyquil.numpy_simulator import NumpyWavefunctionSimulator +from pyquil.simulation import NumpyWavefunctionSimulator from pyquil.quil import DefGate, Program, Pragma from rpcq.messages import TargetDevice from rpcq._utils import RPCErrorError +from pyquil.external.rpcq import CompilerISA from forest.benchmarking.operator_tools.random_operators import haar_rand_unitary from forest.benchmarking.utils import bit_array_to_int @@ -58,9 +59,9 @@ def _naive_program_generator(qc: QuantumComputer, qubits: Sequence[int], prog.measure(qubit, ro[idx]) # restrict compilation to chosen qubits - isa_dict = qc.device.get_isa().to_dict() - single_qs = isa_dict['1Q'] - two_qs = isa_dict['2Q'] + isa = qc.quantum_processor.to_compiler_isa() + single_qs = isa.qubits + two_qs = isa.edges new_1q = {} for key, val in single_qs.items(): @@ -72,10 +73,10 @@ def _naive_program_generator(qc: QuantumComputer, qubits: Sequence[int], if int(q1) in qubits and int(q2) in qubits: new_2q[key] = val - new_isa = {'1Q': new_1q, '2Q': new_2q} + new_isa = CompilerISA.parse_obj({'1Q': new_1q, '2Q': new_2q}) new_compiler = copy(qc.compiler) - new_compiler.target_device = TargetDevice(isa=new_isa, specs=qc.device.get_specs().to_dict()) + new_compiler.target_device = TargetDevice(isa=new_isa.dict(by_alias=True), specs={}) # try to compile with the restricted qubit topology try: native_quil = new_compiler.quil_to_native_quil(prog) @@ -192,7 +193,7 @@ def sample_rand_circuits_for_heavy_out(qc: QuantumComputer, # run the program num_shots many times program.wrap_in_numshots_loop(num_shots) executable = qc.compiler.native_quil_to_executable(program) - results = qc.run(executable) + results = qc.run(executable).readout_data.get('ro') # classically simulate model circuit represented by the perms and gates for heavy outputs heavy_outputs = collect_heavy_outputs(wfn_sim, permutations, gates) diff --git a/forest/benchmarking/readout.py b/forest/benchmarking/readout.py index c1cb2b43..56276855 100644 --- a/forest/benchmarking/readout.py +++ b/forest/benchmarking/readout.py @@ -50,7 +50,7 @@ def estimate_confusion_matrix(qc: QuantumComputer, qubit: int, num_shots: int = ro_zero = zero_meas.declare("ro", "BIT", 1) zero_meas += MEASURE(qubit, ro_zero[0]) zero_meas.wrap_in_numshots_loop(num_shots) - should_be_0 = qc.run(qc.compile(zero_meas)) + should_be_0 = qc.run(qc.compile(zero_meas)).readout_data.get('ro') # prepare one and measure; repeat shots number of times one_meas = Program() @@ -58,7 +58,7 @@ def estimate_confusion_matrix(qc: QuantumComputer, qubit: int, num_shots: int = ro_one = one_meas.declare("ro", "BIT", 1) one_meas += MEASURE(qubit, ro_one[0]) one_meas.wrap_in_numshots_loop(num_shots) - should_be_1 = qc.run(qc.compile(one_meas)) + should_be_1 = qc.run(qc.compile(one_meas)).readout_data.get('ro') p00 = 1 - np.mean(should_be_0) p11 = np.mean(should_be_1) @@ -154,17 +154,17 @@ def estimate_joint_confusion_in_set(qc: QuantumComputer, qubits: Sequence[int] = if use_param_program: # specify bitstring in parameterization at run-time - results = qc.run(executable, - memory_map={reg_name: [float(b) for b in bitstring]}) + executable.write_memory(region_name=reg_name, value=[float(b) for b in bitstring]) + else: # generate program that measures given bitstring on group, and append to start bitstring_program = program_start + bitstring_prep(group, bitstring, append_measure=True) bitstring_program.wrap_in_numshots_loop(shots=num_shots) executable = qc.compiler.native_quil_to_executable(bitstring_program) - results = qc.run(executable) # update confusion matrix + results = qc.run(executable).readout_data.get('ro') for result in results: base = np.array([2 ** i for i in reversed(range(joint_group_size))]) observed = np.sum(base * result) @@ -298,8 +298,8 @@ def estimate_joint_reset_confusion(qc: QuantumComputer, qubits: Sequence[int] = # try preparation at most 10 times. for _ in range(10): # prepare the given bitstring and measure - result = qc.run(prep_executable, - memory_map={reg_name: [float(b) for b in bitstring]}) + prep_executable.write_memory(region_name=reg_name, value=[float(b) for b in bitstring]) + result = qc.run(prep_executable).readout_data.get('ro') # if the preparation is successful, move on to reset. if np.array_equal(result[0], bitstring): @@ -317,7 +317,7 @@ def estimate_joint_reset_confusion(qc: QuantumComputer, qubits: Sequence[int] = for idx, qubit in enumerate(group): reset_measure_program += MEASURE(qubit, ro[idx]) executable = qc.compiler.native_quil_to_executable(reset_measure_program) - results = qc.run(executable) + results = qc.run(executable).readout_data.get('ro') # update confusion matrix for result in results: diff --git a/forest/benchmarking/tests/conftest.py b/forest/benchmarking/tests/conftest.py index 4a70bde1..1f02cf89 100644 --- a/forest/benchmarking/tests/conftest.py +++ b/forest/benchmarking/tests/conftest.py @@ -1,16 +1,12 @@ import os -from requests.exceptions import RequestException import pytest -from unittest.mock import create_autospec, Mock - -from pyquil.api import WavefunctionSimulator, ForestConnection, QVMConnection, get_benchmarker -from pyquil.api._errors import UnknownApiError -from pyquil.paulis import sX +from httpx import RequestError from pyquil import Program, get_qc -from pyquil.gates import I, MEASURE -from pyquil.device import ISA, Device - +from pyquil.api import WavefunctionSimulator, BenchmarkConnection +from pyquil.gates import I +from pyquil.paulis import sX +from pyquil.quantum_processor import NxQuantumProcessor PATH = os.path.dirname(os.path.realpath(__file__)) @@ -18,13 +14,9 @@ @pytest.fixture(scope='module') def test_qc(): import networkx as nx - from requests.exceptions import RequestException - from rpcq.messages import PyQuilExecutableResponse from forest.benchmarking.compilation import basic_compile - from pyquil.api import ForestConnection, QuantumComputer, QVM - from pyquil.api._compiler import _extract_attribute_dictionary_from_program - from pyquil.api._qac import AbstractCompiler - from pyquil.device import NxDevice + from pyquil.api import QuantumComputer, QVM + from pyquil.api._compiler import AbstractCompiler from pyquil.gates import I class BasicQVMCompiler(AbstractCompiler): @@ -33,99 +25,50 @@ def quil_to_native_quil(self, program: Program, protoquil=None): return basic_compile(program) def native_quil_to_executable(self, nq_program: Program): - return PyQuilExecutableResponse( - program=nq_program.out(), - attributes=_extract_attribute_dictionary_from_program(nq_program)) + return nq_program + try: qc = QuantumComputer( name='testing-qc', - qam=QVM(connection=ForestConnection(), random_seed=52), - device=NxDevice(nx.complete_graph(2)), - compiler=BasicQVMCompiler(), + qam=QVM(random_seed=52), + compiler=BasicQVMCompiler( + quantum_processor=NxQuantumProcessor(nx.complete_graph(2)), + timeout=10.0, + client_configuration=None, + ), ) - qc.run_and_measure(Program(I(0)), trials=1) + qc.run(Program(I(0))) return qc - except (RequestException, TimeoutError) as e: + except (RequestError, TimeoutError) as e: return pytest.skip("This test requires a running local QVM: {}".format(e)) @pytest.fixture(scope='module') def qvm(): try: - qc = get_qc('9q-square-qvm') - qc.compiler.client.timeout = 10 - qc.run_and_measure(Program(I(0)), trials=1) + qc = get_qc('9q-square-qvm', compiler_timeout=10.0) + qc.run(Program(I(0))) return qc - except (RequestException, TimeoutError) as e: + except (RequestError, TimeoutError) as e: return pytest.skip("This test requires a running local QVM and quilc: {}".format(e)) @pytest.fixture(scope='session') -def forest(): - try: - connection = ForestConnection() - connection._wavefunction(Program(I(0)), 52) - return connection - except (RequestException, UnknownApiError) as e: - return pytest.skip("This test requires a Forest connection: {}".format(e)) - - -@pytest.fixture(scope='session') -def wfn(forest): - return WavefunctionSimulator(connection=forest) - - -@pytest.fixture(scope='session') -def cxn(): - # DEPRECATED - try: - cxn = QVMConnection(endpoint='http://localhost:5000') - cxn.run(Program(I(0), MEASURE(0, 0)), [0]) - return cxn - except RequestException as e: - return pytest.skip("This test requires a running local QVM: {}".format(e)) +def wfn(): + return WavefunctionSimulator() @pytest.fixture(scope='session') def benchmarker(): try: - benchmarker = get_benchmarker(timeout=10) + benchmarker = BenchmarkConnection(timeout=10) benchmarker.apply_clifford_to_pauli(Program(I(0)), sX(0)) return benchmarker - except (RequestException, TimeoutError) as e: + except (RequestError, TimeoutError) as e: return pytest.skip("This test requires a running local benchmarker endpoint (ie quilc): {}" .format(e)) -@pytest.fixture(scope='session') -def mock_get_devices(): - # Generated from ISA.to_dict. - acorn = {'1Q': {'0': {}, '1': {}, '2': {}, '3': {'dead': True}, '4': {}, '5': {}, '6': {}, '7': {}, '8': {}, - '9': {}, '10': {}, '11': {}, '12': {}, '13': {}, '14': {}, '15': {}, '16': {}, '17': {}, '18': {}, - '19': {}}, - '2Q': {'0-5': {}, '0-6': {}, '1-6': {}, '1-7': {}, '2-7': {}, '2-8': {}, '4-9': {}, '5-10': {}, '6-11': {}, - '7-12': {}, '8-13': {}, '9-14': {}, '10-15': {}, '10-16': {}, '11-16': {}, '11-17': {}, '12-17': {}, - '12-18': {}, '13-18': {}, '13-19': {}, '14-19': {}}} - agave = {'1Q': {'0': {}, '1': {}, '2': {}, '3': {}, '4': {}, '5': {}, '6': {}, '7': {}}, - '2Q': {'0-1': {}, '1-2': {}, '2-3': {}, '3-4': {}, '4-5': {}, '5-6': {}, '6-7': {}, '7-0': {}}} - mock_acorn = Mock(spec=Device) - mock_agave = Mock(spec=Device) - mock_acorn.isa = ISA.from_dict(acorn) - mock_agave.isa = ISA.from_dict(agave) - - # Make sure we are matching the signature. - mocked_function = create_autospec(get_devices) - - def side_effect(as_dict=True): - if as_dict: - return {'19Q-Acorn': mock_acorn, - '8Q-Agave': mock_agave} - else: - return {acorn, agave} - mocked_function.side_effect = side_effect - return mocked_function - - def pytest_addoption(parser): parser.addoption( "--runslow", action="store_true", default=False, help="run slow tests" diff --git a/forest/benchmarking/tests/test_calculational.py b/forest/benchmarking/tests/test_calculational.py index cef4cd18..6c689f3c 100644 --- a/forest/benchmarking/tests/test_calculational.py +++ b/forest/benchmarking/tests/test_calculational.py @@ -1,13 +1,14 @@ import numpy as np -from pyquil.gate_matrices import I -from forest.benchmarking.tests.test_superoperator_transformations import (amplitude_damping_choi, - ONE_STATE, - rho_out) +from pyquil.simulation.matrices import I + from forest.benchmarking.operator_tools.apply_superoperator import apply_choi_matrix_2_state from forest.benchmarking.operator_tools.calculational import (partial_trace, outer_product, inner_product, sqrtm_psd) +from forest.benchmarking.tests.test_superoperator_transformations import (amplitude_damping_choi, + ONE_STATE, + rho_out) def test_partial_trace(): @@ -52,4 +53,4 @@ def test_sqrtm_psd(): np.allclose(sqrtm_psd(A), A) # sqrt(A) * sqrt(A) = A A = np.array([[1, 2], [2, 4]]) - np.allclose(sqrtm_psd(A) @ sqrtm_psd(A), A) \ No newline at end of file + np.allclose(sqrtm_psd(A) @ sqrtm_psd(A), A) diff --git a/forest/benchmarking/tests/test_classical_logic_primites.py b/forest/benchmarking/tests/test_classical_logic_primites.py index 9fd1be2e..d52d2d6e 100644 --- a/forest/benchmarking/tests/test_classical_logic_primites.py +++ b/forest/benchmarking/tests/test_classical_logic_primites.py @@ -31,7 +31,7 @@ def test_majority_gate(qvm): for q in range(3): prog += MEASURE(q, ro[q]) exe = qvm.compiler.native_quil_to_executable(prog) - result = qvm.run(exe) + result = qvm.run(exe).readout_data.get('ro') assert tuple(result[0]) == true_truth_table[key] @@ -59,7 +59,7 @@ def test_unmajority_add_gate(qvm): for q in range(3): prog += MEASURE(q, ro[q]) exe = qvm.compiler.native_quil_to_executable(prog) - result = qvm.run(exe) + result = qvm.run(exe).readout_data.get('ro') assert tuple(result[0]) == true_truth_table[key] @@ -87,7 +87,7 @@ def test_composition_of_majority_and_unmajority_gates(qvm): for q in range(3): prog += MEASURE(q, ro[q]) exe = qvm.compiler.native_quil_to_executable(prog) - result = qvm.run(exe) + result = qvm.run(exe).readout_data.get('ro') assert tuple(result[0]) == true_truth_table[key] @@ -118,7 +118,7 @@ def test_CNOT_in_X_basis(qvm): for q in range(2): prog += MEASURE(q, ro[q]) exe = qvm.compiler.native_quil_to_executable(prog) - result = qvm.run(exe) + result = qvm.run(exe).readout_data.get('ro') assert tuple(result[0]) == true_truth_table[key] @@ -153,5 +153,5 @@ def test_CCNOT_in_X_basis(qvm): for q in range(3): prog += MEASURE(q, ro[q]) exe = qvm.compiler.native_quil_to_executable(prog) - result = qvm.run(exe) + result = qvm.run(exe).readout_data.get('ro') assert tuple(result[0]) == true_truth_table[key] diff --git a/forest/benchmarking/tests/test_compose_superoperators.py b/forest/benchmarking/tests/test_compose_superoperators.py index 0f53aa4a..a60c4089 100644 --- a/forest/benchmarking/tests/test_compose_superoperators.py +++ b/forest/benchmarking/tests/test_compose_superoperators.py @@ -1,9 +1,10 @@ import numpy as np -from pyquil.gate_matrices import I, X, H -from forest.benchmarking.tests.test_superoperator_transformations import amplitude_damping_kraus -from forest.benchmarking.operator_tools.superoperator_transformations import kraus2superop +from pyquil.simulation.matrices import I, X, H + from forest.benchmarking.operator_tools.compose_superoperators import (compose_channel_kraus, tensor_channel_kraus) +from forest.benchmarking.operator_tools.superoperator_transformations import kraus2superop +from forest.benchmarking.tests.test_superoperator_transformations import amplitude_damping_kraus def bit_flip_kraus(p): diff --git a/forest/benchmarking/tests/test_direct_fidelity_estimation.py b/forest/benchmarking/tests/test_direct_fidelity_estimation.py index af98994c..3e216c13 100755 --- a/forest/benchmarking/tests/test_direct_fidelity_estimation.py +++ b/forest/benchmarking/tests/test_direct_fidelity_estimation.py @@ -6,7 +6,7 @@ from pyquil import Program from pyquil.api import BenchmarkConnection from pyquil.gates import * -from pyquil.numpy_simulator import NumpyWavefunctionSimulator +from pyquil.simulation import NumpyWavefunctionSimulator from forest.benchmarking.observable_estimation import _one_q_state_prep from numpy.testing import assert_almost_equal, assert_allclose diff --git a/forest/benchmarking/tests/test_distance_measures.py b/forest/benchmarking/tests/test_distance_measures.py index ad05205a..3911f8f5 100644 --- a/forest/benchmarking/tests/test_distance_measures.py +++ b/forest/benchmarking/tests/test_distance_measures.py @@ -7,11 +7,11 @@ import numpy as np import pytest import os -from pyquil.gate_matrices import I as I_MAT -from pyquil.gate_matrices import X as X_MAT -from pyquil.gate_matrices import Y as Y_MAT -from pyquil.gate_matrices import H as H_MAT -from pyquil.gate_matrices import CNOT as CNOT_MAT +from pyquil.simulation.matrices import I as I_MAT +from pyquil.simulation.matrices import X as X_MAT +from pyquil.simulation.matrices import Y as Y_MAT +from pyquil.simulation.matrices import H as H_MAT +from pyquil.simulation.matrices import CNOT as CNOT_MAT numpy.random.seed(7) # seed random number generation for all calls to rand_ops diff --git a/forest/benchmarking/tests/test_observable_estimation.py b/forest/benchmarking/tests/test_observable_estimation.py index d38910af..6c625c59 100644 --- a/forest/benchmarking/tests/test_observable_estimation.py +++ b/forest/benchmarking/tests/test_observable_estimation.py @@ -1,16 +1,12 @@ -import functools -import itertools import random random.seed(1) # seed random number generation for all calls to rand_ops -import numpy as np -from operator import mul import pytest from pyquil.quilbase import Pragma -from pyquil import Program, get_qc +from pyquil import get_qc from pyquil.gates import * -from pyquil.api import WavefunctionSimulator, QVMConnection -from pyquil.paulis import sI, sX, sY, sZ, PauliSum, PauliTerm +from pyquil.api import WavefunctionSimulator +from pyquil.paulis import sX, sY, sZ, PauliSum from forest.benchmarking.observable_estimation import * from forest.benchmarking.observable_estimation import _OneQState,\ _max_tpb_overlap, _max_weight_operator, _max_weight_state, _one_q_sic_prep @@ -220,7 +216,7 @@ def _random_1q_gate(qubit): yield prog -def test_estimate_observables_many_progs(forest): +def test_estimate_observables_many_progs(): # for "random programs" calculate wfn.expectation see if operator estimation gets it right expts = [ ExperimentSetting(TensorProductState(), o1 * o2) @@ -259,7 +255,7 @@ def test_append(): assert (len(str(suite))) > 0 -def test_no_complex_coeffs(forest): +def test_no_complex_coeffs(): qc = get_qc('2q-qvm') qc.qam.random_seed = 1 suite = ObservablesExperiment([ExperimentSetting(TensorProductState(), 1.j * sY(0))], program=Program(X(0))) @@ -437,7 +433,7 @@ def _expt_settings_diagonal_in_tpb(es1: ExperimentSetting, es2: ExperimentSettin assert not _expt_settings_diagonal_in_tpb(expt_setting2, expt_setting4) -def test_identity(forest): +def test_identity(): qc = get_qc('2q-qvm') qc.qam.random_seed = 1 suite = ObservablesExperiment([ExperimentSetting(plusZ(0), 0.123 * sI(0))], @@ -446,7 +442,7 @@ def test_identity(forest): assert result.expectation == 0.123 -def test_sic_process_tomo(forest): +def test_sic_process_tomo(): qc = get_qc('2q-qvm') qc.qam.random_seed = 1 process = Program(X(0)) @@ -463,7 +459,7 @@ def test_sic_process_tomo(forest): assert len(results) == 4 * 4 -def test_estimate_observables_symmetrize(forest): +def test_estimate_observables_symmetrize(): """ Symmetrization alone should not change the outcome on the QVM """ @@ -485,7 +481,7 @@ def test_estimate_observables_symmetrize(forest): assert np.abs(res.expectation) < 0.1 -def test_estimate_observables_symmetrize_calibrate(forest): +def test_estimate_observables_symmetrize_calibrate(): """ Symmetrization + calibration should not change the outcome on the QVM """ @@ -509,7 +505,7 @@ def test_estimate_observables_symmetrize_calibrate(forest): assert np.abs(res.expectation) < 0.1 -def test_estimate_observables_zero_expectation(forest): +def test_estimate_observables_zero_expectation(): """ Testing case when expectation value of observable should be close to zero """ @@ -555,7 +551,7 @@ def test_ratio_variance_array(): np.testing.assert_allclose(ab_ratio_var, np.array([0.028125, 0.0028125, 0.00028125])) -def test_estimate_observables_uncalibrated_asymmetric_readout(forest): +def test_estimate_observables_uncalibrated_asymmetric_readout(): qc = get_qc('1q-qvm') qc.qam.random_seed = 1 expt1 = ExperimentSetting(TensorProductState(plusX(0)), sX(0)) @@ -579,7 +575,7 @@ def test_estimate_observables_uncalibrated_asymmetric_readout(forest): assert np.isclose(np.mean(expect_arr[2::3]), expected_expectation_z_basis, atol=3e-2) -def test_estimate_observables_uncalibrated_symmetric_readout(forest): +def test_estimate_observables_uncalibrated_symmetric_readout(): # qc = get_qc('1q-qvm') qc.qam.random_seed = 1 @@ -606,7 +602,7 @@ def test_estimate_observables_uncalibrated_symmetric_readout(forest): assert np.isclose(np.mean(uncalibr_e[2::3]), expected_expectation_z_basis, atol=3e-2) -def test_estimate_observables_calibrated_symmetric_readout(forest): +def test_estimate_observables_calibrated_symmetric_readout(): # expecting the result +1 for calibrated readout qc = get_qc('1q-qvm') expt1 = ExperimentSetting(TensorProductState(plusX(0)), sX(0)) @@ -630,7 +626,7 @@ def test_estimate_observables_calibrated_symmetric_readout(forest): np.testing.assert_allclose(results, 1.0, atol=3e-2) -def test_estimate_observables_result_zero_symmetrization_calibration(forest): +def test_estimate_observables_result_zero_symmetrization_calibration(): # expecting expectation value to be 0 with symmetrization/calibration qc = get_qc('9q-qvm') expt1 = ExperimentSetting(TensorProductState(plusX(0)), sZ(0)) @@ -661,7 +657,7 @@ def test_estimate_observables_result_zero_symmetrization_calibration(forest): np.testing.assert_allclose(raw_results, 0.0, atol=3e-2) -def test_estimate_observables_result_zero_no_noisy_readout(forest): +def test_estimate_observables_result_zero_no_noisy_readout(): # expecting expectation value to be 0 with no symmetrization/calibration # and no noisy readout qc = get_qc('9q-qvm') @@ -684,7 +680,7 @@ def test_estimate_observables_result_zero_no_noisy_readout(forest): np.testing.assert_allclose(results, 0.0, atol=3e-2) -def test_estimate_observables_result_zero_no_symm_calibr(forest): +def test_estimate_observables_result_zero_no_symm_calibr(): # expecting expectation value to be nonzero with symmetrization/calibration qc = get_qc('9q-qvm') qc.qam.random_seed = 1 @@ -710,7 +706,7 @@ def test_estimate_observables_result_zero_no_symm_calibr(forest): np.testing.assert_allclose(results, expected_result, atol=3e-2) -def test_estimate_observables_2q_readout_error_one_measured(forest): +def test_estimate_observables_2q_readout_error_one_measured(): # 2q readout errors, but only 1 qubit measured qc = get_qc('9q-qvm') qc.qam.random_seed = 1 @@ -739,7 +735,7 @@ def test_estimate_observables_2q_readout_error_one_measured(forest): assert np.isclose(np.mean(cal_e), 0.849, atol=3e-2) -def test_estimate_observables_inherit_noise_errors(forest): +def test_estimate_observables_inherit_noise_errors(): qc = get_qc('3q-qvm') qc.qam.random_seed = 1 # specify simplest experiments @@ -782,7 +778,7 @@ def test_estimate_observables_inherit_noise_errors(forest): assert calibr_prog3.out() == Program(expected_prog).out() -def test_expectations_sic0(forest): +def test_expectations_sic0(): qc = get_qc('1q-qvm') expt1 = ExperimentSetting(SIC0(0), sX(0)) expt2 = ExperimentSetting(SIC0(0), sY(0)) @@ -804,7 +800,7 @@ def test_expectations_sic0(forest): np.testing.assert_allclose(results, expected_results, atol=3e-2) -def test_expectations_sic1(forest): +def test_expectations_sic1(): qc = get_qc('1q-qvm') expt1 = ExperimentSetting(SIC1(0), sX(0)) expt2 = ExperimentSetting(SIC1(0), sY(0)) @@ -826,7 +822,7 @@ def test_expectations_sic1(forest): np.testing.assert_allclose(results, expected_results, atol=3e-2) -def test_expectations_sic2(forest): +def test_expectations_sic2(): qc = get_qc('1q-qvm') expt1 = ExperimentSetting(SIC2(0), sX(0)) expt2 = ExperimentSetting(SIC2(0), sY(0)) @@ -850,7 +846,7 @@ def test_expectations_sic2(forest): np.testing.assert_allclose(results, expected_results, atol=3e-2) -def test_expectations_sic3(forest): +def test_expectations_sic3(): qc = get_qc('1q-qvm') expt1 = ExperimentSetting(SIC3(0), sX(0)) expt2 = ExperimentSetting(SIC3(0), sY(0)) @@ -874,7 +870,7 @@ def test_expectations_sic3(forest): np.testing.assert_allclose(results, expected_results, atol=3e-2) -def test_sic_conditions(forest): +def test_sic_conditions(): """ Test that the SIC states indeed yield SIC-POVMs """ @@ -914,7 +910,7 @@ def test_sic_conditions(forest): assert np.isclose(np.trace(proj_a.dot(proj_b)), 1 / 3) -def test_estimate_observables_grouped_expts(forest): +def test_estimate_observables_grouped_expts(): qc = get_qc('3q-qvm') # this more explicitly uses the list-of-lists-of-ExperimentSettings in ObservablesExperiment # create experiments in different groups @@ -952,7 +948,7 @@ def _point_channel_fidelity_estimate(v, dim=2): return (1.0 + np.sum(v) + dim) / (dim * (dim + 1)) -def test_bit_flip_channel_fidelity(forest): +def test_bit_flip_channel_fidelity(): """ We use Eqn (5) of https://arxiv.org/abs/quant-ph/0701138 to compare the fidelity """ @@ -992,7 +988,7 @@ def test_bit_flip_channel_fidelity(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_depolarizing_channel_fidelity(forest): +def test_depolarizing_channel_fidelity(): """ We use Eqn (5) of https://arxiv.org/abs/quant-ph/0701138 to compare the fidelity """ @@ -1033,7 +1029,7 @@ def test_depolarizing_channel_fidelity(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_unitary_channel_fidelity(forest): +def test_unitary_channel_fidelity(): """ We use Eqn (5) of https://arxiv.org/abs/quant-ph/0701138 to compare the fidelity """ @@ -1068,7 +1064,7 @@ def test_unitary_channel_fidelity(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_bit_flip_channel_fidelity_readout_error(forest): +def test_bit_flip_channel_fidelity_readout_error(): """ We use Eqn (5) of https://arxiv.org/abs/quant-ph/0701138 to compare the fidelity """ @@ -1112,7 +1108,7 @@ def test_bit_flip_channel_fidelity_readout_error(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_depolarizing_channel_fidelity_readout_error(forest): +def test_depolarizing_channel_fidelity_readout_error(): """ We use Eqn (5) of https://arxiv.org/abs/quant-ph/0701138 to compare the fidelity """ @@ -1157,7 +1153,7 @@ def test_depolarizing_channel_fidelity_readout_error(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_unitary_channel_fidelity_readout_error(forest): +def test_unitary_channel_fidelity_readout_error(): """ We use Eqn (5) of https://arxiv.org/abs/quant-ph/0701138 to compare the fidelity """ @@ -1197,7 +1193,7 @@ def test_unitary_channel_fidelity_readout_error(forest): @pytest.mark.slow -def test_2q_unitary_channel_fidelity_readout_error(forest): +def test_2q_unitary_channel_fidelity_readout_error(): """ We use Eqn (5) of https://arxiv.org/abs/quant-ph/0701138 to compare the fidelity This tests if our dimensionality factors are correct, even in the presence @@ -1257,7 +1253,7 @@ def test_2q_unitary_channel_fidelity_readout_error(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_measure_1q_observable_raw_expectation(forest): +def test_measure_1q_observable_raw_expectation(): # testing that we get correct raw expectation in terms of readout errors qc = get_qc('1q-qvm') expt = ExperimentSetting(TensorProductState(plusZ(0)), sZ(0)) @@ -1285,7 +1281,7 @@ def test_measure_1q_observable_raw_expectation(forest): np.testing.assert_allclose(result, expected_result, atol=3e-2) -def test_measure_1q_observable_raw_variance(forest): +def test_measure_1q_observable_raw_variance(): # testing that we get correct raw std_err in terms of readout errors qc = get_qc('1q-qvm') expt = ExperimentSetting(TensorProductState(plusZ(0)), sZ(0)) @@ -1314,7 +1310,7 @@ def test_measure_1q_observable_raw_variance(forest): np.testing.assert_allclose(result, expected_result, atol=3e-2) -def test_measure_1q_observable_calibration_expectation(forest): +def test_measure_1q_observable_calibration_expectation(): # testing that we get correct calibration expectation in terms of readout errors qc = get_qc('1q-qvm') expt = ExperimentSetting(TensorProductState(plusZ(0)), sZ(0)) @@ -1342,7 +1338,7 @@ def test_measure_1q_observable_calibration_expectation(forest): np.testing.assert_allclose(result, expected_result, atol=3e-2) -def test_measure_1q_observable_calibration_variance(forest): +def test_measure_1q_observable_calibration_variance(): # testing that we get correct calibration std_err in terms of readout errors qc = get_qc('1q-qvm') expt = ExperimentSetting(TensorProductState(plusZ(0)), sZ(0)) @@ -1371,7 +1367,7 @@ def test_measure_1q_observable_calibration_variance(forest): np.testing.assert_allclose(result, expected_result, atol=3e-2) -def test_uncalibrated_asymmetric_readout_nontrivial_1q_state(forest): +def test_uncalibrated_asymmetric_readout_nontrivial_1q_state(): qc = get_qc('1q-qvm') expt = ExperimentSetting(TensorProductState(), sZ(0)) # pick some random value for RX rotation @@ -1398,7 +1394,7 @@ def test_uncalibrated_asymmetric_readout_nontrivial_1q_state(forest): assert np.isclose(np.mean(expect_arr), expected_expectation, atol=3e-2) -def test_uncalibrated_symmetric_readout_nontrivial_1q_state(forest): +def test_uncalibrated_symmetric_readout_nontrivial_1q_state(): qc = get_qc('1q-qvm') qc.qam.random_seed = 1 expt = ExperimentSetting(TensorProductState(), sZ(0)) @@ -1428,7 +1424,7 @@ def test_uncalibrated_symmetric_readout_nontrivial_1q_state(forest): assert np.isclose(np.mean(expect_arr), expected_expectation, atol=3e-2) -def test_calibrated_symmetric_readout_nontrivial_1q_state(forest): +def test_calibrated_symmetric_readout_nontrivial_1q_state(): qc = get_qc('1q-qvm') qc.qam.random_seed = 1 expt = ExperimentSetting(TensorProductState(), sZ(0)) @@ -1461,7 +1457,7 @@ def test_calibrated_symmetric_readout_nontrivial_1q_state(forest): assert np.isclose(np.mean(expect_arr), expected_expectation, atol=3e-2) -def test_measure_2q_observable_raw_statistics(forest): +def test_measure_2q_observable_raw_statistics(): # testing that we get correct exhaustively symmetrized statistics # in terms of readout errors # Note: this only tests for exhaustive symmetrization in the presence @@ -1509,7 +1505,7 @@ def test_measure_2q_observable_raw_statistics(forest): np.testing.assert_allclose(result_std_err, simulated_std_err, atol=3e-2) -def test_raw_statistics_2q_nontrivial_nonentangled_state(forest): +def test_raw_statistics_2q_nontrivial_nonentangled_state(): # testing that we get correct exhaustively symmetrized statistics # in terms of readout errors, even for non-trivial 2q nonentangled states # Note: this only tests for exhaustive symmetrization in the presence @@ -1580,7 +1576,7 @@ def test_raw_statistics_2q_nontrivial_nonentangled_state(forest): np.testing.assert_allclose(result_std_err, simulated_std_err, atol=3e-2) -def test_raw_statistics_2q_nontrivial_entangled_state(forest): +def test_raw_statistics_2q_nontrivial_entangled_state(): # testing that we get correct exhaustively symmetrized statistics # in terms of readout errors, even for non-trivial 2q entangled states # Note: this only tests for exhaustive symmetrization in the presence @@ -1645,7 +1641,7 @@ def _point_state_fidelity_estimate(v, dim=2): return (1.0 + np.sum(v)) / dim -def test_bit_flip_state_fidelity(forest): +def test_bit_flip_state_fidelity(): qc = get_qc('1q-qvm') qc.qam.random_seed = 1 # prepare experiment setting @@ -1680,7 +1676,7 @@ def test_bit_flip_state_fidelity(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_dephasing_state_fidelity(forest): +def test_dephasing_state_fidelity(): qc = get_qc('1q-qvm') # prepare experiment setting expt = ExperimentSetting(TensorProductState(), sZ(0)) @@ -1713,7 +1709,7 @@ def test_dephasing_state_fidelity(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_depolarizing_state_fidelity(forest): +def test_depolarizing_state_fidelity(): qc = get_qc('1q-qvm') # prepare experiment setting expt = ExperimentSetting(TensorProductState(), sZ(0)) @@ -1748,7 +1744,7 @@ def test_depolarizing_state_fidelity(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_unitary_state_fidelity(forest): +def test_unitary_state_fidelity(): qc = get_qc('1q-qvm') # prepare experiment setting expt = ExperimentSetting(TensorProductState(), sZ(0)) @@ -1777,7 +1773,7 @@ def test_unitary_state_fidelity(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_bit_flip_state_fidelity_readout_error(forest): +def test_bit_flip_state_fidelity_readout_error(): qc = get_qc('1q-qvm') # prepare experiment setting expt = ExperimentSetting(TensorProductState(), sZ(0)) @@ -1814,7 +1810,7 @@ def test_bit_flip_state_fidelity_readout_error(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_dephasing_state_fidelity_readout_error(forest): +def test_dephasing_state_fidelity_readout_error(): qc = get_qc('1q-qvm') # prepare experiment setting expt = ExperimentSetting(TensorProductState(), sZ(0)) @@ -1850,7 +1846,7 @@ def test_dephasing_state_fidelity_readout_error(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_depolarizing_state_fidelity_readout_error(forest): +def test_depolarizing_state_fidelity_readout_error(): qc = get_qc('1q-qvm') # prepare experiment setting expt = ExperimentSetting(TensorProductState(), sZ(0)) @@ -1888,7 +1884,7 @@ def test_depolarizing_state_fidelity_readout_error(forest): np.testing.assert_allclose(expected_fidelity, estimated_fidelity, atol=3e-2) -def test_unitary_state_fidelity_readout_error(forest): +def test_unitary_state_fidelity_readout_error(): qc = get_qc('1q-qvm') # prepare experiment setting expt = ExperimentSetting(TensorProductState(), sZ(0)) diff --git a/forest/benchmarking/tests/test_process_tomography.py b/forest/benchmarking/tests/test_process_tomography.py index 7a45088c..e98d2308 100644 --- a/forest/benchmarking/tests/test_process_tomography.py +++ b/forest/benchmarking/tests/test_process_tomography.py @@ -9,9 +9,9 @@ ObservablesExperiment, \ _one_q_state_prep from pyquil import Program -from pyquil import gate_matrices as mat +from pyquil.simulation import matrices as mat from pyquil.gates import CNOT, X, H -from pyquil.numpy_simulator import NumpyWavefunctionSimulator +from pyquil.simulation import NumpyWavefunctionSimulator def wfn_estimate_observables(n_qubits, tomo_expt: ObservablesExperiment): diff --git a/forest/benchmarking/tests/test_project_superoperators.py b/forest/benchmarking/tests/test_project_superoperators.py index 98f44034..4ebe68cf 100644 --- a/forest/benchmarking/tests/test_project_superoperators.py +++ b/forest/benchmarking/tests/test_project_superoperators.py @@ -1,8 +1,7 @@ -import numpy as np -from pyquil.gate_matrices import I, X, Y, Z, H, CNOT -from forest.benchmarking.operator_tools.superoperator_transformations import kraus2choi -from forest.benchmarking.operator_tools.validate_superoperator import * +from pyquil.simulation.matrices import I, X, Y, Z, H, CNOT + from forest.benchmarking.operator_tools.project_superoperators import * +from forest.benchmarking.operator_tools.validate_superoperator import * def test_proj_to_cp(): diff --git a/forest/benchmarking/tests/test_quantum_volume.py b/forest/benchmarking/tests/test_quantum_volume.py index d59446fe..4c693a3d 100644 --- a/forest/benchmarking/tests/test_quantum_volume.py +++ b/forest/benchmarking/tests/test_quantum_volume.py @@ -1,6 +1,6 @@ import numpy as np import warnings -from pyquil.numpy_simulator import NumpyWavefunctionSimulator +from pyquil.simulation import NumpyWavefunctionSimulator from forest.benchmarking.quantum_volume import * from forest.benchmarking.quantum_volume import _naive_program_generator @@ -42,7 +42,7 @@ def test_qv_get_results_by_depth(qvm): program.wrap_in_numshots_loop(n_shots) executable = qvm.compiler.native_quil_to_executable(program) - results = qvm.run(executable) + results = qvm.run(executable).readout_data.get('ro') ckt_results.append(results) heavy_outputs = collect_heavy_outputs(wfn_sim, permutations, gates) diff --git a/forest/benchmarking/tests/test_readout.py b/forest/benchmarking/tests/test_readout.py index 59288a20..351ce3f6 100644 --- a/forest/benchmarking/tests/test_readout.py +++ b/forest/benchmarking/tests/test_readout.py @@ -2,7 +2,6 @@ import numpy as np from pyquil import Program -from pyquil.device import gates_in_isa from pyquil.gates import I, RX, CNOT, MEASURE from pyquil.noise import decoherence_noise_with_asymmetric_ro @@ -36,7 +35,7 @@ def test_get_flipped_program(): def test_readout_confusion_matrix_consistency(qvm): - noise_model = decoherence_noise_with_asymmetric_ro(gates=gates_in_isa(qvm.device.get_isa())) + noise_model = decoherence_noise_with_asymmetric_ro(qvm.quantum_processor.to_compiler_isa()) qvm.qam.noise_model = noise_model qvm.qam.random_seed = 1 num_shots = 500 @@ -74,7 +73,7 @@ def test_readout_confusion_matrix_consistency(qvm): def test_reset_confusion_consistency(qvm): - noise_model = decoherence_noise_with_asymmetric_ro(gates=gates_in_isa(qvm.device.get_isa())) + noise_model = decoherence_noise_with_asymmetric_ro(qvm.quantum_processor.to_compiler_isa()) qvm.qam.noise_model = noise_model qvm.qam.random_seed = 1 num_trials = 10 diff --git a/forest/benchmarking/tests/test_ripple_carry_adder.py b/forest/benchmarking/tests/test_ripple_carry_adder.py index 714e6a68..0461a335 100644 --- a/forest/benchmarking/tests/test_ripple_carry_adder.py +++ b/forest/benchmarking/tests/test_ripple_carry_adder.py @@ -21,7 +21,7 @@ def test_one_bit_addition(qvm): for key, value in true_truth_table.items(): adder_prog = adder([key[0]], [key[1]], [2], [1], 0, 3) exe = qvm.compile(adder_prog) - result = qvm.run(exe) + result = qvm.run(exe).readout_data.get('ro') assert tuple(result[0]) == value @@ -38,6 +38,6 @@ def test_one_bit_addition_X_basis(qvm): for key, value in true_truth_table.items(): adder_prog = adder([key[0]], [key[1]], [2], [1], 0, 3, in_x_basis=True) exe = qvm.compile(adder_prog) - result = qvm.run(exe) + result = qvm.run(exe).readout_data.get('ro') - assert tuple(result[0]) == value \ No newline at end of file + assert tuple(result[0]) == value diff --git a/forest/benchmarking/tests/test_state_tomography.py b/forest/benchmarking/tests/test_state_tomography.py index 443842b3..c270b442 100644 --- a/forest/benchmarking/tests/test_state_tomography.py +++ b/forest/benchmarking/tests/test_state_tomography.py @@ -5,7 +5,7 @@ from forest.benchmarking.tomography import generate_state_tomography_experiment, _R, \ iterative_mle_state_estimate, estimate_variance, linear_inv_state_estimate, do_tomography from pyquil.gates import I, H, CZ -from pyquil.numpy_simulator import NumpyWavefunctionSimulator +from pyquil.simulation import NumpyWavefunctionSimulator from forest.benchmarking.observable_estimation import estimate_observables, ExperimentResult, \ ExperimentSetting, zeros_state, calibrate_observable_estimates from pyquil.paulis import sI, sZ, sX diff --git a/forest/benchmarking/tests/test_superoperator_transformations.py b/forest/benchmarking/tests/test_superoperator_transformations.py index 67475ec9..41c1647b 100644 --- a/forest/benchmarking/tests/test_superoperator_transformations.py +++ b/forest/benchmarking/tests/test_superoperator_transformations.py @@ -1,5 +1,5 @@ -import numpy as np -from pyquil.gate_matrices import X, Y, Z, H +from pyquil.simulation.matrices import X, Z, H + from forest.benchmarking.operator_tools.superoperator_transformations import * diff --git a/forest/benchmarking/tests/test_validate_operators.py b/forest/benchmarking/tests/test_validate_operators.py index 837fbac4..361ab631 100644 --- a/forest/benchmarking/tests/test_validate_operators.py +++ b/forest/benchmarking/tests/test_validate_operators.py @@ -1,6 +1,6 @@ import pytest import numpy as np -from pyquil.gate_matrices import X, Y, Z, H +from pyquil.simulation.matrices import X, Y, Z, H from forest.benchmarking.operator_tools.random_operators import haar_rand_unitary from forest.benchmarking.operator_tools.validate_operator import * diff --git a/forest/benchmarking/tests/test_validate_superoperator.py b/forest/benchmarking/tests/test_validate_superoperator.py index e209fe78..2cff6334 100644 --- a/forest/benchmarking/tests/test_validate_superoperator.py +++ b/forest/benchmarking/tests/test_validate_superoperator.py @@ -1,4 +1,4 @@ -from pyquil.gate_matrices import H +from pyquil.simulation.matrices import H import forest.benchmarking.operator_tools.random_operators as rand_ops from forest.benchmarking.operator_tools.superoperator_transformations import chi2choi from forest.benchmarking.tests.test_superoperator_transformations import ( diff --git a/forest/benchmarking/tomography.py b/forest/benchmarking/tomography.py index 8659090c..737f6b64 100644 --- a/forest/benchmarking/tomography.py +++ b/forest/benchmarking/tomography.py @@ -9,7 +9,7 @@ from pyquil import Program from pyquil.api import QuantumComputer -from pyquil.unitary_tools import lifted_pauli as pauli2matrix, lifted_state_operator as state2matrix +from pyquil.simulation.tools import lifted_pauli as pauli2matrix, lifted_state_operator as state2matrix import forest.benchmarking.distance_measures as dm from forest.benchmarking.utils import all_traceless_pauli_terms diff --git a/forest/benchmarking/utils.py b/forest/benchmarking/utils.py index b46bf2fb..899f8719 100644 --- a/forest/benchmarking/utils.py +++ b/forest/benchmarking/utils.py @@ -1,18 +1,18 @@ import itertools from collections import OrderedDict -from random import random, seed -from typing import Sequence, List, Set, Tuple from datetime import date, datetime -from git import Repo +from random import random, seed +from typing import Sequence, Tuple + import numpy as np -from numpy import pi import pandas as pd - +from git import Repo +from numpy import pi +from pyquil.api import QuantumComputer from pyquil.gates import I, RX, RY, RZ, H, MEASURE -from pyquil.gate_matrices import X, Y, Z from pyquil.paulis import PauliTerm from pyquil.quil import Program -from pyquil.api import QuantumComputer +from pyquil.simulation.matrices import X, Y, Z def is_pos_pow_two(x: int) -> bool: diff --git a/requirements.txt b/requirements.txt index 46e8103e..05d4ec3f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -pyquil>=2.14 +pyquil>=3.0.0,<4.0.0 numpy networkx pandas @@ -29,4 +29,4 @@ sphinx_rtd_theme nbsphinx # for testing examples -nbval \ No newline at end of file +nbval