Skip to content

Commit c7ef2a1

Browse files
[Bugfixes] Solving some issues for qemulator MPS release (#73)
* Added angles vs angle parameter handling to hybrid_engine_old * The reset methods in simulators must return self. * If import fails, create dummy NoneType simulator * Updated bindings of CoinToss * Removed filter of simulation parameters, since it is not necessary * Using a more sensible import approach in the test file * Debug message converted to info message.
1 parent 6a5f1f9 commit c7ef2a1

File tree

7 files changed

+56
-79
lines changed

7 files changed

+56
-79
lines changed

python/pecos/engines/hybrid_engine_old.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ def run_gate(state, output, symbol: str, locations, **params):
214214

215215
if params.get("simulate_gate", True):
216216
for location in locations:
217+
218+
if params.get("angles") and len(params["angles"]) == 1:
219+
params.update({"angle": params["angles"][0]})
220+
elif "angle" in params and "angles" not in params:
221+
params["angles"] = (params["angle"],)
222+
217223
try:
218224
result = state.bindings[symbol](state, location, **params)
219225
except KeyError:

python/pecos/simulators/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@
3030

3131
from pecos.simulators.projectq.state import ProjectQSim # wrapper for ProjectQ sim
3232
except ImportError:
33-
pass
33+
ProjectQSim = None
3434

3535
# Attempt to import optional Qulacs package
36-
try: # noqa: SIM105
36+
try:
3737
from pecos.simulators.qulacs.state import Qulacs # wrapper for Qulacs sim
3838
except ImportError:
39-
pass
39+
Qulacs = None
4040

4141
# Attempt to import optional QuEST package
42-
try: # noqa: SIM105
42+
try:
4343
from pecos.simulators.quest.state import QuEST # wrapper for QuEST sim
4444
except ImportError:
45-
pass
45+
QuEST = None
4646

4747
# Attempt to import optional cuquantum and cupy packages
4848
try:
@@ -52,4 +52,5 @@
5252
from pecos.simulators.custatevec.state import CuStateVec # wrapper for cuQuantum's cuStateVec
5353
from pecos.simulators.mps_pytket import MPS # MPS implementation from pytket-cutensornet
5454
except ImportError:
55-
pass
55+
CuStateVec = None
56+
MPS = None

python/pecos/simulators/cointoss/bindings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@
1616

1717
gate_dict = {
1818
"Init": ignore_gate,
19+
"Init +Z": ignore_gate,
20+
"Init -Z": ignore_gate,
21+
"init |0>": ignore_gate,
22+
"init |1>": ignore_gate,
23+
"leak": ignore_gate,
24+
"leak |0>": ignore_gate,
25+
"leak |1>": ignore_gate,
26+
"unleak |0>": ignore_gate,
27+
"unleak |1>": ignore_gate,
1928
"Measure": measure,
29+
"measure Z": measure,
2030
"I": ignore_gate,
2131
"X": ignore_gate,
2232
"Y": ignore_gate,
@@ -25,12 +35,19 @@
2535
"RY": ignore_gate,
2636
"RZ": ignore_gate,
2737
"R1XY": ignore_gate,
38+
"RXY1Q": ignore_gate,
2839
"SX": ignore_gate,
2940
"SXdg": ignore_gate,
41+
"SqrtX": ignore_gate,
42+
"SqrtXd": ignore_gate,
3043
"SY": ignore_gate,
3144
"SYdg": ignore_gate,
45+
"SqrtY": ignore_gate,
46+
"SqrtYd": ignore_gate,
3247
"SZ": ignore_gate,
3348
"SZdg": ignore_gate,
49+
"SqrtZ": ignore_gate,
50+
"SqrtZd": ignore_gate,
3451
"H": ignore_gate,
3552
"F": ignore_gate,
3653
"Fdg": ignore_gate,
@@ -48,6 +65,7 @@
4865
"SYY": ignore_gate,
4966
"SYYdg": ignore_gate,
5067
"SZZ": ignore_gate,
68+
"SqrtZZ": ignore_gate,
5169
"SZZdg": ignore_gate,
5270
"SWAP": ignore_gate,
5371
}

python/pecos/simulators/cointoss/state.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ def __init__(self, num_qubits, prob=0.5, seed=None):
4848
self.bindings = bindings.gate_dict
4949
self.num_qubits = num_qubits
5050
self.prob = prob
51+
52+
def reset(self):
53+
"""Reset the quantum state for another run without reinitializing."""
54+
# Do nothing, this simulator does not keep a state!
55+
return self

python/pecos/simulators/mps_pytket/state.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ def __init__(self, num_qubits, **mps_params) -> None:
6060
# Initialise the MPS on state |0>
6161
self.reset()
6262

63-
def reset(self) -> None:
63+
def reset(self) -> StateTN:
6464
"""Reset the quantum state to all 0 for another run."""
6565
qubits = [Qubit(q) for q in range(self.num_qubits)]
6666
self.mps = MPSxGate(self.libhandle, qubits, self.config)
67-
self.mps._logger.debug("Resetting MPS...") # noqa: SLF001
67+
self.mps._logger.info("Resetting MPS...") # noqa: SLF001
68+
return self
6869

6970
def __del__(self) -> None:
7071
# CuPy will release GPU memory when the variable ``self.mps`` is no longer

python/pecos/simulators/quantum_simulator.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,7 @@ def __init__(self, backend: str | object | None = None, **params) -> None:
4646
self.num_qubits = None
4747
self.state = None
4848
self.backend = backend
49-
50-
if backend in {"MPS", "mps"}:
51-
self.qsim_params = {
52-
k: v
53-
for k, v in params.items()
54-
if k
55-
in {
56-
"chi",
57-
"truncation_fidelity",
58-
"float_precision",
59-
"value_of_zero",
60-
"loglevel",
61-
"seed",
62-
}
63-
} # These are listed in the docs for ``Config`` of pytket-cutensornet
64-
else:
65-
self.qsim_params = {}
49+
self.qsim_params = params
6650

6751
def reset(self):
6852
self.num_qubits = None

tests/integration/state_sim_tests/test_statevec.py

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,72 +19,34 @@
1919
from pecos.simulators.sim_class_types import StateVector
2020

2121
import json
22-
from importlib.metadata import version
2322
from pathlib import Path
2423

2524
import numpy as np
2625
import pytest
27-
from packaging.version import parse as vparse
2826
from pecos.circuits import QuantumCircuit
2927
from pecos.engines.hybrid_engine import HybridEngine
3028
from pecos.error_models.generic_error_model import GenericErrorModel
31-
from pecos.simulators import BasicSV
32-
33-
# Try to import the requirements for Qulacs
34-
try:
35-
from pecos.simulators import Qulacs
36-
37-
qulacs_ready = True
38-
except ImportError:
39-
qulacs_ready = False
40-
41-
# Try to import the requirements for QuEST
42-
try:
43-
from pecos.simulators import QuEST
44-
45-
quest_ready = True
46-
except ImportError:
47-
quest_ready = False
48-
49-
# Try to import the requirements for CuStateVec
50-
try:
51-
import cuquantum
52-
53-
imported_cuquantum = vparse(cuquantum._version.__version__) >= vparse("24.03.0") # noqa: SLF001
54-
import cupy as cp # noqa: F401
55-
56-
imported_cupy = vparse(version("cupy")) >= vparse("10.4.0")
57-
from pecos.simulators import CuStateVec
58-
59-
custatevec_ready = imported_cuquantum and imported_cupy
60-
except (ImportError, AttributeError):
61-
custatevec_ready = False
62-
63-
# Try to import pytket-cutensornet
64-
try:
65-
import pytket.extensions.cutensornet # noqa: F401
66-
from pecos.simulators import MPS
29+
from pecos.simulators import (
30+
MPS,
31+
BasicSV,
32+
CuStateVec,
33+
QuEST,
34+
Qulacs,
35+
)
6736

68-
mps_ready = imported_cuquantum and imported_cupy # Same requirements as CuStateVec
69-
except ImportError:
70-
mps_ready = False
37+
str_to_sim = {
38+
"BasicSV": BasicSV,
39+
"Qulacs": Qulacs,
40+
"QuEST": QuEST,
41+
"CuStateVec": CuStateVec,
42+
"MPS": MPS,
43+
}
7144

7245

7346
def check_dependencies(simulator) -> Callable[[int], StateVector]:
74-
if simulator == "BasicSV":
75-
sim = BasicSV
76-
elif simulator == "Qulacs" and qulacs_ready:
77-
sim = Qulacs
78-
elif simulator == "QuEST" and quest_ready:
79-
sim = QuEST
80-
elif simulator == "CuStateVec" and custatevec_ready:
81-
sim = CuStateVec
82-
elif simulator == "MPS" and mps_ready:
83-
sim = MPS
84-
else:
47+
if simulator not in str_to_sim or str_to_sim[simulator] is None:
8548
pytest.skip(f"Requirements to test {simulator} are not met.")
86-
87-
return sim
49+
return str_to_sim[simulator]
8850

8951

9052
def verify(simulator, qc: QuantumCircuit, final_vector: np.ndarray) -> None:

0 commit comments

Comments
 (0)