Skip to content

Commit 100ce91

Browse files
add docstrings;fix jax to_dlpack;add with_prob impl for stabilizercircuit.measure
1 parent b6d4985 commit 100ce91

File tree

8 files changed

+392
-91
lines changed

8 files changed

+392
-91
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111
- Add `circuit.amplitude_before()` method to return the corresponding tensornetwork nodes.
1212

13+
- Add `with_prob` for `stabilizercircuit.measure()`.
14+
1315
### Fixed
1416

1517
- Fix the nodes order in contraction by giving each node a global `_stable_id_`.
1618

19+
- Fix `to_dlpack` for jax version >= 0.7.
20+
1721
## v1.2.1
1822

1923
### Fixed

tensorcircuit/backends/jax_backend.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,14 @@ def from_dlpack(self, a: Any) -> Tensor:
442442
def to_dlpack(self, a: Tensor) -> Any:
443443
import jax.dlpack
444444

445-
return jax.dlpack.to_dlpack(a)
445+
try:
446+
return jax.dlpack.to_dlpack(a) # type: ignore
447+
except AttributeError: # jax >v0.7
448+
# jax.dlpack.to_dlpack was deprecated in JAX v0.6.0 and removed in JAX v0.7.0.
449+
# Please use the newer DLPack API based on __dlpack__ and __dlpack_device__ instead.
450+
# Typically, you can pass a JAX array directly to the `from_dlpack` function of
451+
# another framework without using `to_dlpack`.
452+
return a.__dlpack__()
446453

447454
def set_random_state(
448455
self, seed: Optional[Union[int, PRNGKeyArray]] = None, get_only: bool = False

tensorcircuit/circuit.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,25 @@ def depolarizing2(
231231
pz: float,
232232
status: Optional[float] = None,
233233
) -> float:
234+
"""
235+
Apply a depolarizing channel to the circuit in a Monte Carlo way.
236+
For each call, one of the Pauli gates (X, Y, Z) or an Identity gate is applied to the qubit
237+
at the given index based on the probabilities `px`, `py`, and `pz`.
238+
239+
:param index: The index of the qubit to apply the depolarizing channel on.
240+
:type index: int
241+
:param px: The probability of applying an X gate.
242+
:type px: float
243+
:param py: The probability of applying a Y gate.
244+
:type py: float
245+
:param pz: The probability of applying a Z gate.
246+
:type pz: float
247+
:param status: A random number between 0 and 1 to determine which gate to apply. If None,
248+
a random number is generated automatically. Defaults to None.
249+
:type status: Optional[float], optional
250+
:return: Returns 0.0. The function modifies the circuit in place.
251+
:rtype: float
252+
"""
234253
if status is None:
235254
status = backend.implicit_randu()[0]
236255
g = backend.cond(
@@ -323,6 +342,35 @@ def unitary_kraus2(
323342
status: Optional[float] = None,
324343
name: Optional[str] = None,
325344
) -> Tensor:
345+
"""
346+
Apply a unitary Kraus channel to the circuit using a Monte Carlo approach. This method is functionally
347+
similar to `unitary_kraus` but uses `backend.switch` for selecting the Kraus operator, which can have
348+
different performance characteristics on some backends.
349+
350+
A random Kraus operator from the provided list is applied to the circuit based on the given probabilities.
351+
This method is jittable and suitable for simulating noisy quantum circuits where the noise is represented
352+
by unitary Kraus operators.
353+
354+
.. warning::
355+
This method may have issues with `vmap` due to potential concurrent access locks, potentially related with
356+
`backend.switch`. `unitary_kraus` is generally recommended.
357+
358+
:param kraus: A sequence of `Gate` objects representing the unitary Kraus operators.
359+
:type kraus: Sequence[Gate]
360+
:param index: The qubit indices on which to apply the Kraus channel.
361+
:type index: int
362+
:param prob: A sequence of probabilities corresponding to each Kraus operator. If None, probabilities
363+
are derived from the operators themselves. Defaults to None.
364+
:type prob: Optional[Sequence[float]], optional
365+
:param status: A random number between 0 and 1 to determine which Kraus operator to apply. If None,
366+
a random number is generated automatically. Defaults to None.
367+
:type status: Optional[float], optional
368+
:param name: An optional name for the operation. Defaults to None.
369+
:type name: Optional[str], optional
370+
:return: A tensor indicating which Kraus operator was applied.
371+
:rtype: Tensor
372+
"""
373+
326374
# dont use, has issue conflicting with vmap, concurrent access lock emerged
327375
# potential issue raised from switch
328376
# general impl from Monte Carlo trajectory depolarizing above

0 commit comments

Comments
 (0)