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

Refactor simulator #213

Merged
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8c1293c
refactored simulator
JamesB-1qbit Aug 3, 2022
798f627
removed target argument from simulator base class
JamesB-1qbit Aug 3, 2022
aac6b7e
changed Simulator to function
JamesB-1qbit Aug 16, 2022
d47d907
added target_qiskit_device
JamesB-1qbit Aug 19, 2022
1bce352
import fixes
JamesB-1qbit Aug 19, 2022
3a68e94
added cirq QVM and qsimcirq backends
JamesB-1qbit Aug 22, 2022
5db101f
added test for rainbow
JamesB-1qbit Aug 29, 2022
920d6ff
moved QiskitDevice to target_qiskit, added docstrings
JamesB-1qbit Sep 15, 2022
abf2e38
added docstrings
JamesB-1qbit Sep 15, 2022
285a8d3
fixes for failing tests
JamesB-1qbit Sep 27, 2022
1fe9ba5
simulatorbase backend_info as property
JamesB-1qbit Sep 27, 2022
42a841b
updated targets qsimcirq and qiskit
JamesB-1qbit Sep 28, 2022
ed64677
utilize histogram for qsimcirq
JamesB-1qbit Sep 28, 2022
e6678a9
add save_measurements to translation functions
JamesB-1qbit Sep 29, 2022
ca1c6a5
removed noisy fake device simulator, removed changes to translate fun…
JamesB-1qbit Sep 29, 2022
a98d0de
first PR review, moved SimulatorBase to own file, backend imports onl…
JamesB-1qbit Oct 3, 2022
cb133f9
docstring and fix for test error
JamesB-1qbit Oct 3, 2022
92c0a75
switch Simulator to Class
JamesB-1qbit Oct 4, 2022
9177b94
note -> TODO
JamesB-1qbit Oct 4, 2022
bbd294a
explicit check for target is None
JamesB-1qbit Oct 4, 2022
450c7b2
updated initialization and docstrings
JamesB-1qbit Oct 5, 2022
e94a63c
test for user provided target simulator
JamesB-1qbit Oct 5, 2022
ea6ff73
explicit support for None target
JamesB-1qbit Oct 5, 2022
51e49e8
class -> Factory function
JamesB-1qbit Oct 6, 2022
d039720
move expect function
JamesB-1qbit Oct 13, 2022
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
7 changes: 4 additions & 3 deletions examples/linq/1.the_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,10 @@
}
],
"source": [
"from tangelo.linq import SUPPORTED_GATES\n",
"from tangelo.linq import get_supported_gates\n",
"\n",
"for backend, gates in SUPPORTED_GATES.items():\n",
"supported_gates = get_supported_gates()\n",
"for backend, gates in supported_gates.items():\n",
" print(f'{backend} : {gates}')"
]
},
Expand Down Expand Up @@ -844,7 +845,7 @@
"# Option2: Assume quantum circuit simulation was performed separately (by this package or different services)\n",
"freqs, _ = sim.simulate(c) # circuit c must be consistent with operator, regarding measurement along non-z axes\n",
"term, coef = tuple(op.terms.items())[0] # This yields ((0, 'Z'),), 1.0\n",
"expval2 = coef * Simulator.get_expectation_value_from_frequencies_oneterm(term, freqs)\n",
"expval2 = coef * sim.get_expectation_value_from_frequencies_oneterm(term, freqs)\n",
JamesB-1qbit marked this conversation as resolved.
Show resolved Hide resolved
"print(expval2)"
]
},
Expand Down
15 changes: 10 additions & 5 deletions examples/linq/2.qpu_connection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -767,24 +767,24 @@
}
],
"source": [
"from tangelo.linq import Simulator\n",
"from tangelo.linq.simulator_base import SimulatorBase\n",
"from tangelo.linq.helpers.circuits.measurement_basis import pauli_string_to_of\n",
"\n",
"# Rescale counts to obtain frequency histogram\n",
"freqs = {k:v/100 for k,v in counts.items()}\n",
"print(f\"Frequencies: {freqs}\")\n",
"\n",
"# Compute expectation value for ZZ operator\n",
"exp_ZZ = Simulator.get_expectation_value_from_frequencies_oneterm(pauli_string_to_of(\"ZZ\"), freqs)\n",
"exp_ZZ = SimulatorBase.get_expectation_value_from_frequencies_oneterm(pauli_string_to_of(\"ZZ\"), freqs)\n",
JamesB-1qbit marked this conversation as resolved.
Show resolved Hide resolved
"print(f\"Expectation value for ZZ operator {exp_ZZ}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "main_release0.3.1",
"display_name": "Python 3.9.7 64-bit ('qsdkmain': venv)",
"language": "python",
"name": "main_release0.3.1"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -796,7 +796,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.9.10"
},
"vscode": {
"interpreter": {
"hash": "95050af2697fca56ed7491a4fb0b04c1282c0de0a7e0a7cacd318a8297b0b1d8"
}
}
},
"nbformat": 4,
Expand Down
15 changes: 14 additions & 1 deletion tangelo/algorithms/variational/tests/test_vqe_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import numpy as np

from tangelo.linq import Simulator
from tangelo.helpers.utils import installed_backends
from tangelo.linq.target import QiskitSimulator
from tangelo.algorithms import BuiltInAnsatze, VQESolver
from tangelo.molecule_library import mol_H2_sto3g, mol_H4_sto3g, mol_H4_cation_sto3g, mol_NaH_sto3g, mol_H4_sto3g_symm
from tangelo.toolboxes.ansatz_generator.uccsd import UCCSD
Expand Down Expand Up @@ -197,9 +199,10 @@ def test_simulate_vsqs_h2(self):
energy = vqe_solver.simulate()
self.assertAlmostEqual(energy, -1.137270, delta=1e-4)

@unittest.skipIf("qiskit" not in installed_backends, "Test Skipped: Backend not available \n")
def test_simulate_h2_qiskit(self):
"""Run VQE on H2 molecule, with UCCSD ansatz, JW qubit mapping, initial
parameters, exact qiskit simulator.
parameters, exact qiskit simulator. Both string and class input.
"""

backend_options = {"target": "qiskit", "n_shots": None, "noise_model": None}
Expand All @@ -212,6 +215,16 @@ def test_simulate_h2_qiskit(self):

self.assertAlmostEqual(energy, -1.13727042117, delta=1e-6)

backend_options = {"target": QiskitSimulator, "n_shots": None, "noise_model": None}
vqe_options = {"molecule": mol_H2_sto3g, "ansatz": BuiltInAnsatze.UCCSD, "qubit_mapping": "jw",
"initial_var_params": [6.28531447e-06, 5.65431626e-02], "verbose": True,
"backend_options": backend_options}
vqe_solver = VQESolver(vqe_options)
vqe_solver.build()
energy = vqe_solver.simulate()

self.assertAlmostEqual(energy, -1.13727042117, delta=1e-6)

def test_simulate_h4(self):
"""Run VQE on H4 molecule, with UCCSD ansatz, JW qubit mapping, initial
parameters, exact simulator.
Expand Down
11 changes: 4 additions & 7 deletions tangelo/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import os
import sys
from importlib import util


class HiddenPrints:
Expand All @@ -34,13 +35,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):


def is_package_installed(package_name):
try:
exec(f"import {package_name}")
# DEBUG print(f'{package_name:16s} :: found')
return True
except ModuleNotFoundError:
# DEBUG print(f'{package_name:16s} :: not found')
return False
"""Check if module is installed without importing."""
JamesB-1qbit marked this conversation as resolved.
Show resolved Hide resolved
spam_spec = util.find_spec(package_name)
return spam_spec is not None


# List all backends and statevector ones supported by Simulator class
Expand Down
Loading