Device
DynamicMemorySimulator
dataclass
DynamicMemorySimulator(
options: PyQrackOptions = _default_pyqrack_args(),
*,
loss_m_result: Measurement = Measurement.One,
rng_state: Generator = np.random.default_rng()
)
Bases: PyQrackSimulatorBase
PyQrack simulator device with dynamic qubit allocation.
This can be used to simulate kernels where the number of qubits is not known ahead of time.
Usage examples
# Define a kernel
@qasm2.main
def main():
q = qasm2.qreg(2)
c = qasm2.creg(2)
qasm2.h(q[0])
qasm2.cx(q[0], q[1])
qasm2.measure(q, c)
return q
# Create the simulator object
sim = DynamicMemorySimulator()
# Execute the kernel
qubits = sim.run(main)
You can also obtain other information from it, such as the state vector:
``` ket = sim.state_vector(main)
from pyqrack.pauli import Pauli expectation_vals = sim.pauli_expectation([Pauli.PauliX, Pauli.PauliI], qubits)
task
task(
kernel: Method[Params, RetType],
args: tuple[Any, ...] = (),
kwargs: dict[str, Any] | None = None,
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kernel
|
Method
|
The kernel method to run. |
required |
args
|
tuple[Any, ...]
|
Positional arguments to pass to the kernel method. |
()
|
kwargs
|
dict[str, Any] | None
|
Keyword arguments to pass to the kernel method. |
None
|
Returns:
Name | Type | Description |
---|---|---|
PyQrackSimulatorTask |
The task object used to track execution. |
Source code in .venv/lib/python3.12/site-packages/bloqade/pyqrack/device.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
PyQrackSimulatorBase
dataclass
PyQrackSimulatorBase(
options: PyQrackOptions = _default_pyqrack_args(),
*,
loss_m_result: Measurement = Measurement.One,
rng_state: Generator = np.random.default_rng()
)
Bases: AbstractSimulatorDevice[PyQrackSimulatorTask]
PyQrack simulation device base class.
options
class-attribute
instance-attribute
options: PyQrackOptions = field(
default_factory=_default_pyqrack_args
)
options (PyQrackOptions): options passed into the pyqrack simulator.
pauli_expectation
staticmethod
pauli_expectation(
pauli: list[Pauli], qubits: list[PyQrackQubit]
) -> float
Returns the expectation value of the given Pauli operator given a list of Pauli operators and qubits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pauli
|
list[Pauli]
|
List of Pauli operators to compute the expectation value for. |
required |
qubits
|
list[PyQrackQubit]
|
List of qubits corresponding to the Pauli operators. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The expectation value of the Pauli operator. |
Source code in .venv/lib/python3.12/site-packages/bloqade/pyqrack/device.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
quantum_state
staticmethod
quantum_state(
qubits: list[PyQrackQubit] | IList[PyQrackQubit, Any],
tol: float = 1e-12,
) -> np.linalg._linalg.EighResult
Extract the reduced density matrix representing the state of a list of qubits from a PyQRack simulator register.
Inputs
qubits: A list of PyQRack qubits to extract the reduced density matrix for tol: The tolerance for density matrix eigenvalues to be considered non-zero.
Outputs: An eigh result containing the eigenvalues and eigenvectors of the reduced density matrix.
Source code in .venv/lib/python3.12/site-packages/bloqade/pyqrack/device.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
reduced_density_matrix
classmethod
reduced_density_matrix(
qubits: list[PyQrackQubit] | IList[PyQrackQubit, Any],
tol: float = 1e-12,
) -> np.ndarray
Extract the reduced density matrix representing the state of a list of qubits from a PyQRack simulator register.
Inputs
qubits: A list of PyQRack qubits to extract the reduced density matrix for tol: The tolerance for density matrix eigenvalues to be considered non-zero.
Outputs: A dense 2^n x 2^n numpy array representing the reduced density matrix.
Source code in .venv/lib/python3.12/site-packages/bloqade/pyqrack/device.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
state_vector
state_vector(
kernel: Method[Params, RetType],
args: tuple[Any, ...] = (),
kwargs: dict[str, Any] | None = None,
) -> list[complex]
Runs task and returns the state vector.
Source code in .venv/lib/python3.12/site-packages/bloqade/pyqrack/device.py
122 123 124 125 126 127 128 129 |
|
StackMemorySimulator
dataclass
StackMemorySimulator(
options: PyQrackOptions = _default_pyqrack_args(),
*,
loss_m_result: Measurement = Measurement.One,
rng_state: Generator = np.random.default_rng(),
min_qubits: int = 0
)
Bases: PyQrackSimulatorBase
PyQrack simulator device with preallocated stack of qubits.
This can be used to simulate kernels where the number of qubits is known ahead of time.
Usage examples
# Define a kernel
@qasm2.main
def main():
q = qasm2.qreg(2)
c = qasm2.creg(2)
qasm2.h(q[0])
qasm2.cx(q[0], q[1])
qasm2.measure(q, c)
return q
# Create the simulator object
sim = StackMemorySimulator(min_qubits=2)
# Execute the kernel
qubits = sim.run(main)
You can also obtain other information from it, such as the state vector:
ket = sim.state_vector(main)
from pyqrack.pauli import Pauli
expectation_vals = sim.pauli_expectation([Pauli.PauliX, Pauli.PauliI], qubits)
task
task(
kernel: Method[Params, RetType],
args: tuple[Any, ...] = (),
kwargs: dict[str, Any] | None = None,
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kernel
|
Method
|
The kernel method to run. |
required |
args
|
tuple[Any, ...]
|
Positional arguments to pass to the kernel method. |
()
|
kwargs
|
dict[str, Any] | None
|
Keyword arguments to pass to the kernel method. |
None
|
Returns:
Name | Type | Description |
---|---|---|
PyQrackSimulatorTask |
The task object used to track execution. |
Source code in .venv/lib/python3.12/site-packages/bloqade/pyqrack/device.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|