Random objects¶
graphix.random_objects module¶
This module provides functions to generate various random objects.
- graphix.random_objects.rand_herm(sz: IntLike, rng: Generator | None = None, *, stacklevel: int = 1) npt.NDArray[np.complex128][source]¶
Generate random hermitian matrix of size sz*sz.
- graphix.random_objects.rand_unit(sz: IntLike, rng: Generator | None = None, *, stacklevel: int = 1) npt.NDArray[np.complex128][source]¶
Generate haar random unitary matrix of size sz*sz.
- graphix.random_objects.rand_dm(dim: IntLike, rng: Generator | None = None, rank: IntLike | None = None, *, stacklevel: int = 1) npt.NDArray[np.complex128][source]¶
Generate random density matrices (positive semi-definite matrices with unit trace).
Returns either a
graphix.sim.density_matrix.DensityMatrixor anp.ndarraydepending on the parameter dm_dtype.- Parameters:
dim (int) – Linear dimension of the (square) matrix
rank (int, optional) – Rank of the density matrix (1 = pure state). If not specified then sent to dim (maximal rank). Defaults to None
- Returns:
the density matrix in the specified format.
- Return type:
DensityMatrix | np.ndarray
Note
Thanks to Ulysse Chabaud.
- graphix.random_objects.rand_gauss_cpx_mat(dim: IntLike, rng: Generator | None = None, sig: _SIG = None, *, stacklevel: int = 1) npt.NDArray[np.complex128][source]¶
Return a square array of standard normal complex random variates.
Code from QuTiP: https://qutip.org/docs/4.0.2/modules/qutip/random_objects.html
- Parameters:
dim (int) – Linear dimension of the (square) matrix
sig (float | Literal["ginibre"] | None) – standard deviation of random variates.
sig = 'ginibredraws from the Ginibre ensemble ie sig = 1 / sqrt(2 * dim).
- graphix.random_objects.rand_channel_kraus(dim: int, rng: Generator | None = None, rank: int | None = None, sig: _SIG = None, *, stacklevel: int = 1) KrausChannel[source]¶
Return a random
graphix.sim.channels.KrausChannelobject of given dimension and rank.Following the method of [KNPPZ21] Kukulski, Nechita, Pawela, Puchała, Życzkowsk https://arxiv.org/pdf/2011.02994.pdf
- Parameters:
dim (int) – Linear dimension of the (square) matrix of each Kraus operator. Only square operators so far.
rank (int (default to full rank dimension**2)) – Choi rank ie the number of Kraus operators. Must be between one and dim**2.
sig (see rand_cpx)
- graphix.random_objects.rand_pauli_channel_kraus(dim: int, rng: Generator | None = None, rank: int | None = None, *, stacklevel: int = 1) KrausChannel[source]¶
Return a random Kraus channel operator.
- graphix.random_objects.rand_gate(nqubits: int, depth: int, pairs: Iterable[tuple[int, int]], rng: Generator | None = None, *, use_rzz: bool = False, stacklevel: int = 1) Circuit[source]¶
Return a random gate composed of single-qubit rotations and entangling operations.
- Parameters:
nqubits (int) – Number of qubits in the circuit.
depth (int) – Depth of alternating rotation and entangling layers.
pairs (Iterable[tuple[int, int]]) – Pairs of qubits used for entangling operations.
rng (numpy.random.Generator, optional) – Random number generator used to sample rotation angles. If
None, a default generator is created.use_rzz (bool, optional) – If
Trueuse \(R_{ZZ}\) gates as entanglers instead of CNOT.
- Returns:
The generated random circuit.
- Return type:
- graphix.random_objects.rand_circuit(nqubits: int, depth: int, rng: Generator | None = None, *, use_cz: bool = True, use_rzz: bool = False, use_ccx: bool = False, parameters: Iterable[Parameter] | None = None, stacklevel: int = 1) Circuit[source]¶
Return a random parameterized circuit used for testing or benchmarking.
- Parameters:
nqubits (int) – Number of qubits in the circuit.
depth (int) – Number of alternating entangling and single-qubit layers.
rng (numpy.random.Generator, optional) – Random number generator. A default generator is created if
None.use_cz (bool, optional) – If
Trueadd CZ gates in each layer (default:True).use_rzz (bool, optional) – If
Trueadd \(R_{ZZ}\) gates in each layer (default:False).use_ccx (bool, optional) – If
Trueadd CCX gates in each layer (default:False).parameters (Iterable[Parameter], optional) – Parameters used for randomly chosen rotation gates.
- Returns:
The generated random circuit.
- Return type:
graphix.rng module¶
This module provides a default random-number generator if None is given.
- graphix.rng.ensure_rng(rng: Generator | None = None, *, stacklevel: int = 1) Generator[source]¶
Return a default random-number generator if
Noneis given.- Parameters:
rng (Generator | None, optional) – If set and not
None, this value is returned. IfNone(the default), the default random-number generator is returned.stacklevel (int, optional) – Stack level to use for warnings. Defaults to 1, meaning that warnings are reported at this function’s call site.
Notes
A warning is issued if the default random-number generator is used.