Skip to content

Commit e920ad6

Browse files
weinbe58Copilot
andauthored
Moving state_vector method to task (#253)
+ Adding a bug fix for `pauli_expectation`. --------- Co-authored-by: Copilot <[email protected]>
1 parent 89ffbab commit e920ad6

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/bloqade/pyqrack/device.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ def state_vector(
6060
kwargs: dict[str, Any] | None = None,
6161
) -> list[complex]:
6262
"""Runs task and returns the state vector."""
63-
task = self.task(kernel, args, kwargs)
64-
task.run()
65-
return task.state.sim_reg.out_ket()
63+
return self.task(kernel, args, kwargs).state_vector()
6664

6765
@staticmethod
6866
def pauli_expectation(pauli: list[Pauli], qubits: list[PyQrackQubit]) -> float:
@@ -96,7 +94,7 @@ def pauli_expectation(pauli: list[Pauli], qubits: list[PyQrackQubit]) -> float:
9694
if len(qubit_ids) != len(set(qubit_ids)):
9795
raise ValueError("Qubits must be unique.")
9896

99-
return sim_reg.pauli_expectation(pauli, qubit_ids)
97+
return sim_reg.pauli_expectation(qubit_ids, pauli)
10098

10199

102100
@dataclass

src/bloqade/pyqrack/task.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypeVar, ParamSpec
1+
from typing import TypeVar, ParamSpec, cast
22
from dataclasses import dataclass
33

44
from bloqade.task import AbstractSimulatorTask
@@ -19,12 +19,20 @@ class PyQrackSimulatorTask(AbstractSimulatorTask[Param, RetType, MemoryType]):
1919
pyqrack_interp: PyQrackInterpreter[MemoryType]
2020

2121
def run(self) -> RetType:
22-
return self.pyqrack_interp.run(
23-
self.kernel,
24-
args=self.args,
25-
kwargs=self.kwargs,
22+
return cast(
23+
RetType,
24+
self.pyqrack_interp.run(
25+
self.kernel,
26+
args=self.args,
27+
kwargs=self.kwargs,
28+
),
2629
)
2730

2831
@property
2932
def state(self) -> MemoryType:
3033
return self.pyqrack_interp.memory
34+
35+
def state_vector(self) -> list[complex]:
36+
"""Returns the state vector of the simulator."""
37+
self.run()
38+
return self.state.sim_reg.out_ket()

0 commit comments

Comments
 (0)