Utility#

supergrad.utils.utility.basis(dimensions, n=None, offset=None)[source]#

Generates the vector representation of a Fock state.

Parameters:
  • dimensions – int or list of ints Number of Fock states in Hilbert space. If a list, then the resultant object will be a tensor product over spaces with those dimensions.

  • n – int or list of ints, optional (default 0 for all dimensions) Integer corresponding to desired number state, defaults to 0 for all dimensions if omitted. The shape must match dimensions, e.g. if dimensions is a list, then n must either be omitted or a list of equal length.

  • offset – int or list of ints, optional (default 0 for all dimensions) The lowest number state that is included in the finite number state representation of the state in the relevant dimension.

Returns:

ndarray representing the requested number state |n>.

Return type:

state

supergrad.utils.utility.compute_average_photon(power: float, freq: float, qc: float | None = None, kappa_c: float | None = None) float[source]#

Computes the average photon number in a cavity.

Note, must specify one and only one of qc and kappa_c.

Parameters:
  • power (float) – the input power, in unit dBm

  • freq (float) – the cavity frequency (normal frequency), in unit GHz

  • qc (float) – the coupling quality factor

  • kappa_c (float) – the coupling decay rate (normal frequency)

Returns:

The average photon number.

Return type:

float

supergrad.utils.utility.const_init(val)[source]#

return an intializer which generates constant value or arrays

Parameters:

val – the constant value

Returns:

A callable of shape, dtype to generate a constant value or array.

supergrad.utils.utility.convert_device_array(x)[source]#

Convert a device array to a list or a number. This is for convert parameters to json compatible format.

supergrad.utils.utility.convert_to_haiku_dict(x)[source]#

Convert input to haiku compatible dictionary. In particular, it will replace input float to the DeviceArray.

supergrad.utils.utility.convert_to_json_compatible(x)[source]#

Convert input to json compatible format. In particular, it will replace DeviceArray or np.array in the input.

supergrad.utils.utility.create_density_init(ar_truncated_dim: ndarray, ar_ix_max: ndarray)[source]#

Create initial density matrices (computational-basis projectors) for an N-partite system.

This function enumerates computational-basis kets and returns their density matrices |ψ⟩⟨ψ| in a batch, ready for Lindblad master equation evolution. It mirrors the functionality of create_state_init but returns density matrices instead of state vectors.

Parameters:
  • ar_truncated_dim – the number of bands in each tensor

  • ar_ix_max – [N,2] array as (qubit index, band maximum band index) to iterate all bands from 0 to maximum. [[0, 2], [1,2]] will create | 00> | 01> | 10> | 11> states.

Returns:

np.ndarray, shape (B, D, D)

Batch of computational-basis density matrices, one per enumerated basis state. Here D = prod(ar_truncated_dim), B = number of enumerated basis states. Each density matrix represents a pure computational-basis state |i⟩⟨i|.

ar_ix: np.ndarray, shape (B, N)

The same basis-index array returned by create_state_init.

Return type:

rho_list

supergrad.utils.utility.create_state(ar_truncated_dim: ndarray, ar_ix: ndarray)[source]#

Creates N-Q basis.

Parameters:
  • ar_truncated_dim – the number of bands in each tensor

  • ar_ix – the index of band in each tensor

Returns:

Array as the state | i1 i2 i3…>

supergrad.utils.utility.create_state_init(ar_truncated_dim: ndarray, ar_ix_max: ndarray | List[List[int]])[source]#

Creates initial states in a N-Q system.

Parameters:
  • ar_truncated_dim – the number of bands in each tensor

  • ar_ix_max – [N,2] array as (qubit index, band maximum band index) to iterate all bands from 0 to maximum. [[0, 2], [1,2]] will create | 00> | 01> | 10> | 11> states.

Returns:

a list of initial states

supergrad.utils.utility.dump_params(params, fp)[source]#

Dump parameters to json.

supergrad.utils.utility.fock_dm(dimensions, n=None, offset=None)[source]#

Density matrix representation of a Fock state

Constructed via outer product of supergrad.utils.utility.basis().

Parameters:
  • dimensions – int or list of ints Number of Fock states in Hilbert space. If a list, then the resultant object will be a tensor product over spaces with those dimensions.

  • n – int or list of ints, optional (default 0 for all dimensions) Integer corresponding to desired number state, defaults to 0 for all dimensions if omitted. The shape must match dimensions, e.g. if dimensions is a list, then n must either be omitted or a list of equal length.

  • offset – int or list of ints, optional (default 0 for all dimensions) The lowest number state that is included in the finite number state representation of the state in the relevant dimension.

Returns:

ndarray

Density matrix representation of Fock state.

Return type:

dm

supergrad.utils.utility.ghz_state(n=3)[source]#

Returns the N-qubit GHZ-state.

Parameters:

N – int (default=3) Number of qubits in state

Returns:

N-qubit GHZ-state

Return type:

array

supergrad.utils.utility.identity_wrap(operator: Callable | Array, subsystem_list, subsystem=None, coeff=1, **kwargs) Array[source]#

Wrap given operator in subspace subsystem in identity operators to form full Hilbert-space operator.

Parameters:
  • operator – operator acting in Hilbert space of subsystem

  • subsystem_list (list) – list of all subsystems relevant to the Hilbert space.

  • subsystem (string) – subsystem where diagonal operator is defined. set None if operator is Callable

  • coeff (float) – coefficient of operator

  • kwargs – kwargs for calling operator.

supergrad.utils.utility.load_params(fp)[source]#

Load parameters from json.

supergrad.utils.utility.permute(unitary, dims, order)[source]#

Permutes the tensor structure of a composite object in the given order.

Parameters:
  • unitary (array) – 2d-array of a composite object

  • dims (list) – list of int dimensions of a composite object

  • order (list) – list of int the given order of the tensor structure. Note that len(dims) == len(order), and the order in range [0, len(dims)-1]

supergrad.utils.utility.qutrit_basis()[source]#

Basis states for a three level system (qutrit)

Returns:

array

Array of qutrit basis vectors

Return type:

qstates

supergrad.utils.utility.reduced_unitary(unitary, dims, partial_trace)[source]#

Calculate the partial trace to extract the unitary of target subsystem, when dealing with composite systems.

Parameters:
  • unitary (array) – 2d-array of a composite object

  • dims (list) – list of int dimensions of a composite object

  • partial_trace (list) – list of int the order of subsystem will be traced. Note the order in range [0, len(dims)-1]

supergrad.utils.utility.tensor(*operators)[source]#

Calculates the tensor product of input operators by jax.numpy.kron.

supergrad.utils.utility.tensor_np(*operators)[source]#

Calculates the tensor product of input operators by numpy.kron.

supergrad.utils.utility.tree_print(t)[source]#

Print jax pytree in a human readable way.

Fidelity#

supergrad.utils.fidelity.compute_fidelity_with_1q_rotation_axis(u_target, u_computed, opt_method: str = 'gd', compensation_option='only_vz', options: dict | None = None)[source]#

Compute fidelity, allows for any single qubit rotation before/after

Parameters:
  • u_target (array) – Target unitary

  • u_computed (array) – Unitary

  • opt_method (string) – optimizer, set gd for using the Gradient Descent solver; set lbfgs for using the LBFGS solver, set iswap_vz for performing the iswap gate compensation through virtual Z gates.

  • compensation_option – (string) Set single qubit compensation strategy, should be in [‘no_comp’, ‘only_vz’, ‘arbit_single’]

  • options (dict) – additional keyword arguments to be passed to the jaxopt optimizer.