quimb.gen.operators

Functions for generating quantum operators.

Functions

CNOT([dtype, sparse])

The controlled-not gate.

S_gate([dtype, sparse])

The S-gate (phase gate).

T_gate([dtype, sparse])

The T-gate (pi/8 gate).

U_gate(theta, phi, lamda[, dtype, sparse])

Arbitrary unitary single qubit gate.

Wsqrt(**qu_opts)

R[X + Y](pi / 2).

Xsqrt(**qu_opts)

Rx(pi / 2).

Ysqrt(**qu_opts)

Ry(pi / 2).

Zsqrt(**qu_opts)

Rz(pi / 2).

cX([dtype, sparse])

The controlled-X gate.

cY([dtype, sparse])

The controlled-Y gate.

cZ([dtype, sparse])

The controlled-Z gate.

controlled(s[, dtype, sparse])

Construct a controlled pauli gate for two qubits.

create([n])

The creation operator acting on an n-level system.

destroy([n])

The annihilation operator acting on an n-level system.

fsim(theta, phi[, dtype])

The ‘fermionic simulation’ gate:

hadamard([dtype, sparse])

The Hadamard gate.

ham_XXZ(n, delta[, jxy])

Generate the XXZ-model hamiltonian.

ham_XY(n, jxy, bz, **ham_opts)

Generate the quantum transverse field XY model hamiltonian.

ham_heis(n[, j, b, cyclic, parallel, …])

Constructs the nearest neighbour 1d heisenberg spin-1/2 hamiltonian.

ham_heis_2D(n, m[, j, bz, cyclic, parallel, …])

Construct the 2D spin-1/2 heisenberg model hamiltonian.

ham_hubbard_hardcore(n[, t, V, mu, cyclic, …])

Generate the spinless fermion hopping hamiltonian.

ham_ising(n[, jz, bx])

Generate the quantum transverse field ising model hamiltonian.

ham_j1j2(n[, j1, j2, bz, cyclic, ownership])

Generate the j1-j2 hamiltonian, i.e. next nearest neighbour interactions.

ham_mbl(n, dh[, j, bz, cyclic, seed, …])

Constructs a heisenberg hamiltonian with isotropic coupling and random fields acting on each spin - the many-body localized (MBL) spin hamiltonian.

hamiltonian_builder(fn)

Wrap a function to perform some generic postprocessing and take the kwargs stype and sparse.

iswap([dtype])

num(n, **qu_opts)

The number operator acting on an n-level system.

pauli(xyz[, dim])

Generates the pauli operators for dimension 2 or 3.

phase_gate(phi[, dtype, sparse])

The generalized qubit phase-gate, which adds phase phi to the |1> state.

rotation(phi[, xyz, dtype, sparse])

The single qubit rotation gate.

spin_operator(label[, S])

Generate a general spin-operator.

swap([dim, dtype])

The SWAP operator acting on subsystems of dimension dim.

uniq_perms(xs)

Generate all the unique permutations of sequence xs.

zspin_projector(n[, sz, stype, dtype])

Construct the projector onto spin-z subpspaces.

quimb.gen.operators.CNOT(dtype=<class 'complex'>, sparse=False)[source]

The controlled-not gate.

quimb.gen.operators.Rx(phi, *, xyz='x', dtype=<class 'complex'>, sparse=False)

The single qubit rotation gate.

quimb.gen.operators.Ry(phi, *, xyz='y', dtype=<class 'complex'>, sparse=False)

The single qubit rotation gate.

quimb.gen.operators.Rz(phi, *, xyz='z', dtype=<class 'complex'>, sparse=False)

The single qubit rotation gate.

quimb.gen.operators.S_gate(dtype=<class 'complex'>, sparse=False)[source]

The S-gate (phase gate).

quimb.gen.operators.T_gate(dtype=<class 'complex'>, sparse=False)[source]

The T-gate (pi/8 gate).

quimb.gen.operators.U_gate(theta, phi, lamda, dtype=<class 'complex'>, sparse=False)[source]

Arbitrary unitary single qubit gate.

\[\begin{split}U_3(\theta, \phi, \lambda) = \begin{bmatrix} \cos(\theta / 2) & - e^{i \lambda} \sin(\theta / 2) \\ e^{i \phi} \sin(\theta / 2) & e^{i(\lambda + \phi)}\cos(\theta / 2) \end{bmatrix}\end{split}\]
Parameters
  • theta (float) – Angle between 0 and pi.

  • phi (float) – Angle between 0 and 2 pi.

  • lamba (float) – Angle between 0 and 2 pi.

Returns

U – The unitary matrix, cached.

Return type

(2, 2) array

quimb.gen.operators.Wsqrt(**qu_opts)[source]

R[X + Y](pi / 2).

\[\begin{split}W^{\frac{1}{2}} = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & -\sqrt{i} \\ \sqrt{-i} & 1 \end{bmatrix}\end{split}\]
quimb.gen.operators.Xsqrt(**qu_opts)[source]

Rx(pi / 2).

\[\begin{split}X^{\frac{1}{2}} = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & - i \\ - i & 1 \end{bmatrix}\end{split}\]
quimb.gen.operators.Ysqrt(**qu_opts)[source]

Ry(pi / 2).

\[\begin{split}Y^{\frac{1}{2}} = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & - 1 \\ 1 & 1 \end{bmatrix}\end{split}\]
quimb.gen.operators.Zsqrt(**qu_opts)[source]

Rz(pi / 2).

\[\begin{split}Z^{\frac{1}{2}} = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 - i & 0 \\ 0 & 1 + i \end{bmatrix}\end{split}\]
quimb.gen.operators.cX(dtype=<class 'complex'>, sparse=False)[source]

The controlled-X gate.

quimb.gen.operators.cY(dtype=<class 'complex'>, sparse=False)[source]

The controlled-Y gate.

quimb.gen.operators.cZ(dtype=<class 'complex'>, sparse=False)[source]

The controlled-Z gate.

quimb.gen.operators.controlled(s, dtype=<class 'complex'>, sparse=False)[source]

Construct a controlled pauli gate for two qubits.

Parameters
  • s (str) – Which pauli to use, including ‘not’ aliased to ‘x’.

  • sparse (bool, optional) – Whether to construct a sparse operator.

Returns

C – The controlled two-qubit gate operator.

Return type

immutable operator

quimb.gen.operators.create(n=2, **qu_opts)[source]

The creation operator acting on an n-level system.

quimb.gen.operators.destroy(n=2, **qu_opts)[source]

The annihilation operator acting on an n-level system.

quimb.gen.operators.fsim(theta, phi, dtype=<class 'complex'>, **kwargs)[source]

The ‘fermionic simulation’ gate:

\[\begin{split}\mathrm{fsim}(\theta, \phi) = \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & \cos(\theta) & -i sin(\theta) & 0\\ 0 & -i sin(\theta) & \cos(\theta) & 0\\ 0 & 0 & 0 & \exp(-i \phi) \end{bmatrix}\end{split}\]

Note that theta and phi should be specified in radians and the sign convention with this gate varies. Here for example, fsim(- pi / 2, 0) == iswap().

quimb.gen.operators.hadamard(dtype=<class 'complex'>, sparse=False)[source]

The Hadamard gate.

quimb.gen.operators.ham_XXZ(n, delta, jxy=1.0, **ham_opts)[source]

Generate the XXZ-model hamiltonian. This is a simple alias for ham_heis() with matched X- and Y-interactions and delta Z coupling.

quimb.gen.operators.ham_XY(n, jxy, bz, **ham_opts)[source]

Generate the quantum transverse field XY model hamiltonian. This is a simple alias for ham_heis() with X- and Y-interactions and a Z-field.

quimb.gen.operators.ham_heis(n, j=1.0, b=0.0, cyclic=True, parallel=False, nthreads=None, ownership=None)[source]

Constructs the nearest neighbour 1d heisenberg spin-1/2 hamiltonian.

Parameters
  • n (int) – Number of spins.

  • j (float or tuple(float, float, float), optional) – Coupling constant(s), with convention that positive = antiferromagnetic. Can supply scalar for isotropic coupling or vector (jx, jy, jz).

  • b (float or tuple(float, float, float), optional) – Magnetic field, defaults to z-direction only if tuple not given.

  • cyclic (bool, optional) – Whether to couple the first and last spins.

  • sparse (bool, optional) – Whether to return the hamiltonian in sparse form.

  • stype (str, optional) – What format of sparse operator to return if sparse.

  • parallel (bool, optional) – Whether to build the operator in parallel. By default will do this for n > 16.

  • nthreads (int optional) – How mny threads to use in parallel to build the operator.

  • ownership ((int, int), optional) – If given, which range of rows to generate.

  • kwargs – Supplied to quimbify().

Returns

H – The Hamiltonian.

Return type

immutable operator

quimb.gen.operators.ham_heis_2D(n, m, j=1.0, bz=0.0, cyclic=False, parallel=False, ownership=None)[source]

Construct the 2D spin-1/2 heisenberg model hamiltonian.

Parameters
  • n (int) – The number of rows.

  • m (int) – The number of columns.

  • j (float or (float, float, float), optional) – The coupling strength(s). Isotropic if scalar else if vector (Jx, Jy, Jz) = j.

  • bz (float, optional) – The z direction magnetic field.

  • cyclic (bool, optional) – Whether to use periodic boundary conditions.

  • sparse (bool, optional) – Whether to construct the hamiltonian in sparse form.

  • stype ({'csr', 'csc', 'coo'}, optional) – The sparse format.

  • parallel (bool, optional) – Construct the hamiltonian in parallel. Faster but might use more memory.

  • ownership ((int, int), optional) – If given, which range of rows to generate.

  • kwargs – Supplied to quimbify().

Returns

H – The hamiltonian.

Return type

operator

quimb.gen.operators.ham_hubbard_hardcore(n, t=0.5, V=1.0, mu=1.0, cyclic=True, parallel=False, ownership=None)[source]

Generate the spinless fermion hopping hamiltonian.

Parameters
  • n (int) – The number of sites.

  • t (float, optional) – The hopping energy.

  • V (float, optional) – The interaction energy.

  • mu (float, optional) – The chemical potential - defaults to half-filling.

  • cyclic (bool, optional) – Whether to use periodic boundary conditions.

  • parallel (bool, optional) – Construct the hamiltonian in parallel. Faster but might use more memory.

  • ownership ((int, int), optional) – If given, which range of rows to generate.

  • kwargs – Supplied to quimbify().

Returns

H – The hamiltonian.

Return type

operator

quimb.gen.operators.ham_ising(n, jz=1.0, bx=1.0, **ham_opts)[source]

Generate the quantum transverse field ising model hamiltonian. This is a simple alias for ham_heis() with Z-interactions and an X-field.

quimb.gen.operators.ham_j1j2(n, j1=1.0, j2=0.5, bz=0.0, cyclic=True, ownership=None)[source]

Generate the j1-j2 hamiltonian, i.e. next nearest neighbour interactions.

Parameters
  • n (int) – Number of spins.

  • j1 (float, optional) – Nearest neighbour coupling strength.

  • j2 (float, optional) – Next nearest neighbour coupling strength.

  • bz (float, optional) – B-field strength in z-direction.

  • cyclic (bool, optional) – Cyclic boundary conditions.

  • sparse (bool, optional) – Return hamiltonian as sparse-csr operator.

  • ownership ((int, int), optional) – If given, which range of rows to generate.

  • kwargs – Supplied to quimbify().

Returns

H – The Hamiltonian.

Return type

immutable operator

quimb.gen.operators.ham_mbl(n, dh, j=1.0, bz=0.0, cyclic=True, seed=None, dh_dist='s', dh_dim=1, beta=None, ownership=None)[source]

Constructs a heisenberg hamiltonian with isotropic coupling and random fields acting on each spin - the many-body localized (MBL) spin hamiltonian.

Parameters
  • n (int) – Number of spins.

  • dh (float or (float, float, float)) – Strength of random fields (stdev of gaussian distribution), can be scalar (isotropic noise) or 3-vector for (x, y, z) directions.

  • j (float or (float, float, float), optional) – Coupling strength, can be scalar (isotropic) or 3-vector.

  • bz (float, optional) – Global magnetic field (in z-direction).

  • cyclic (bool, optional) – Whether to use periodic boundary conditions.

  • seed (int, optional) – Number to seed random number generator with.

  • dh_dist ({'g', 's', 'qr'}, optional) –

    Type of random distribution for the noise:

    • ”s”: square, with bounds (-dh, dh)

    • ”g”: gaussian, with standard deviation dh

    • ”qp”: quasi periodic, with amplitude dh and ‘wavenumber’ beta so that the field at site i is dh * cos(2 * pi * beta * i + delta) with delta a random offset between (0, 2 * pi), possibly seeded by seed.

  • dh_dim ({1, 2, 3} or str, optional) – The number of dimensions the noise acts in, or string specifier like 'yz'.

  • beta (float, optional) – The wave number if dh_dist='qr', defaults to the golden ratio``(5**0.5 - 1) / 2``.

  • sparse (bool, optional) – Whether to construct the hamiltonian in sparse form.

  • stype ({'csr', 'csc', 'coo'}, optional) – The sparse format.

  • ownership ((int, int), optional) – If given, which range of rows to generate.

  • kwargs – Supplied to quimbify().

Returns

H – The MBL hamiltonian for spin-1/2.

Return type

operator

See also

MPO_ham_mbl()

quimb.gen.operators.hamiltonian_builder(fn)[source]

Wrap a function to perform some generic postprocessing and take the kwargs stype and sparse. This assumes the core function always builds the hamiltonian in sparse form. The wrapper then:

  1. Checks if the operator is real and, if so, discards imaginary part if no explicity dtype was given

  2. Converts the operator to dense or the correct sparse form

  3. Makes the operator immutable so it can be safely cached

quimb.gen.operators.num(n, **qu_opts)[source]

The number operator acting on an n-level system.

quimb.gen.operators.pauli(xyz, dim=2, **kwargs)[source]

Generates the pauli operators for dimension 2 or 3.

Parameters
  • xyz (str) – Which spatial direction, upper or lower case from {'I', 'X', 'Y', 'Z'}.

  • dim (int, optional) – Dimension of spin operator (e.g. 3 for spin-1), defaults to 2 for spin half.

  • kwargs – Passed to quimbify.

Returns

P – The pauli operator.

Return type

immutable operator

See also

spin_operator()

quimb.gen.operators.phase_gate(phi, dtype=<class 'complex'>, sparse=False)[source]

The generalized qubit phase-gate, which adds phase phi to the |1> state.

quimb.gen.operators.rotation(phi, xyz='Z', dtype=<class 'complex'>, sparse=False)[source]

The single qubit rotation gate.

quimb.gen.operators.spin_operator(label, S=0.5, **kwargs)[source]

Generate a general spin-operator.

Parameters
  • label (str) –

    The type of operator, can be one of six options:
    • {'x', 'X'}, x-spin operator.

    • {'y', 'Y'}, y-spin operator.

    • {'z', 'Z'}, z-spin operator.

    • {'+', 'p'}, Raising operator.

    • {'-', 'm'}, Lowering operator.

    • {'i', 'I'}, identity operator.

  • S (float, optional) – The spin of particle to act on, default to spin-1/2.

  • kwargs – Passed to quimbify().

Returns

S – The spin operator.

Return type

immutable operator

See also

pauli()

Examples

>>> spin_operator('x')
qarray([[0. +0.j, 0.5+0.j],
        [0.5+0.j, 0. +0.j]])
>>> qu.spin_operator('+', S=1)
qarray([[0.        +0.j, 1.41421356+0.j, 0.        +0.j],
        [0.        +0.j, 0.        +0.j, 1.41421356+0.j],
        [0.        +0.j, 0.        +0.j, 0.        +0.j]])
>>> qu.spin_operator('Y', sparse=True)
<2x2 sparse matrix of type '<class 'numpy.complex128'>'
    with 2 stored elements in Compressed Sparse Row format>
quimb.gen.operators.swap(dim=2, dtype=<class 'complex'>, **kwargs)[source]

The SWAP operator acting on subsystems of dimension dim.

quimb.gen.operators.uniq_perms(xs)[source]

Generate all the unique permutations of sequence xs.

Examples

>>> list(uniq_perms('0011'))
[('0', '0', '1', '1'),
 ('0', '1', '0', '1'),
 ('0', '1', '1', '0'),
 ('1', '0', '0', '1'),
 ('1', '0', '1', '0'),
 ('1', '1', '0', '0')]
quimb.gen.operators.zspin_projector(n, sz=0, stype='csr', dtype=<class 'float'>)[source]

Construct the projector onto spin-z subpspaces.

Parameters
  • n (int) – Total size of spin system.

  • sz (float or sequence of floats) – Spin-z value(s) subspace(s) to find projector for.

  • stype (str) – Sparse format of the output operator.

  • dtype ({float, complex}, optional) – The data type of the operator to generate.

Returns

prj – The (non-square) projector onto the specified subspace(s). The subspace size D is given by n choose (n / 2 + s) for each s specified in sz.

Return type

immutable sparse operator, shape (2**n, D)

Examples

>>> zspin_projector(n=2, sz=0).A
array([[0., 0.],
       [1., 0.],
       [0., 1.],
       [0., 0.]]

Project a 9-spin Heisenberg-Hamiltonian into its spin-1/2 subspace:

>>> H = ham_heis(9, sparse=True)
>>> H.shape
(512, 512)
>>> P = zspin_projector(n=9, sz=1 / 2)
>>> H0 = P.T @ H @ P
>>> H0.shape
(126, 126)