LightningQubit

class LightningQubit(wires, *, c_dtype=<class 'numpy.complex128'>, shots=None, seed='global', mcmc=False, kernel_name='Local', num_burnin=100, batch_obs=False)[source]

Bases: Device

PennyLane Lightning Qubit device.

A device that interfaces with C++ to perform fast linear algebra calculations.

Use of this device requires pre-built binaries or compilation from source. Check out the Lightning-Qubit installation guide for more details.

Parameters
  • wires (int) – the number of wires to initialize the device with

  • c_dtype – Datatypes for statevector representation. Must be one of np.complex64 or np.complex128.

  • shots (int) – How many times the circuit should be evaluated (or sampled) to estimate the expectation values. Defaults to None if not specified. Setting to None results in computing statistics like expectation values and variances analytically.

  • seed (Union[str, None, int, array_like[int], SeedSequence, BitGenerator, Generator]) – A seed-like parameter matching that of seed for numpy.random.default_rng, or a request to seed from numpy’s global random number generator. The default, seed="global" pulls a seed from NumPy’s global generator. seed=None will pull a seed from the OS entropy.

  • mcmc (bool) – Determine whether to use the approximate Markov Chain Monte Carlo sampling method when generating samples.

  • kernel_name (str) – name of transition MCMC kernel. The current version supports two kernels: "Local" and "NonZeroRandom". The local kernel conducts a bit-flip local transition between states. The local kernel generates a random qubit site and then generates a random number to determine the new bit at that qubit site. The "NonZeroRandom" kernel randomly transits between states that have nonzero probability.

  • num_burnin (int) – number of MCMC steps that will be dropped. Increasing this value will result in a closer approximation but increased runtime.

  • batch_obs (bool) – Determine whether we process observables in parallel when computing the jacobian. This value is only relevant when the lightning qubit is built with OpenMP.

c_dtype

State vector complex data type.

config

dtype

State vector complex data type.

name

The name of the device.

observables

operations

shots

Default shots for execution workflows containing this device.

tracker

A Tracker that can store information about device executions, shots, batches, intermediate results, or any additional device dependent information.

wires

The device wires.

c_dtype

State vector complex data type.

config = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/xanaduai-pennylane-lightning/envs/latest/lib/python3.9/site-packages/pennylane_lightning/lightning_qubit/lightning_qubit.toml')
dtype

State vector complex data type.

name

The name of the device.

observables = frozenset({'Exp', 'Hadamard', 'Hamiltonian', 'Hermitian', 'Identity', 'LinearCombination', 'PauliX', 'PauliY', 'PauliZ', 'Prod', 'Projector', 'SProd', 'SparseHamiltonian', 'Sum'})
operations = frozenset({'Adjoint(ISWAP)', 'Adjoint(S)', 'Adjoint(SISWAP)', 'Adjoint(SX)', 'Adjoint(T)', 'BlockEncode', 'C(DoubleExcitation)', 'C(DoubleExcitationMinus)', 'C(DoubleExcitationPlus)', 'C(GlobalPhase)', 'C(Hadamard)', 'C(IsingXX)', 'C(IsingXY)', 'C(IsingYY)', 'C(IsingZZ)', 'C(MultiRZ)', 'C(PauliX)', 'C(PauliY)', 'C(PauliZ)', 'C(PhaseShift)', 'C(RX)', 'C(RY)', 'C(RZ)', 'C(Rot)', 'C(S)', 'C(SWAP)', 'C(SingleExcitation)', 'C(SingleExcitationMinus)', 'C(SingleExcitationPlus)', 'C(T)', 'CNOT', 'CPhase', 'CRX', 'CRY', 'CRZ', 'CRot', 'CSWAP', 'CY', 'CZ', 'ControlledPhaseShift', 'ControlledQubitUnitary', 'DiagonalQubitUnitary', 'DoubleExcitation', 'DoubleExcitationMinus', 'DoubleExcitationPlus', 'ECR', 'GlobalPhase', 'Hadamard', 'ISWAP', 'Identity', 'IsingXX', 'IsingXY', 'IsingYY', 'IsingZZ', 'MultiControlledX', 'MultiRZ', 'OrbitalRotation', 'PSWAP', 'PauliX', 'PauliY', 'PauliZ', 'PhaseShift', 'QFT', 'QubitCarry', 'QubitSum', 'QubitUnitary', 'RX', 'RY', 'RZ', 'Rot', 'S', 'SISWAP', 'SQISW', 'SWAP', 'SX', 'SingleExcitation', 'SingleExcitationMinus', 'SingleExcitationPlus', 'T', 'Toffoli'})
shots

Default shots for execution workflows containing this device.

Note that the device itself should always pull shots from the provided QuantumTape and its shots, not from this property. This property is used to provide a default at the start of a workflow.

tracker: Tracker = <pennylane.tracker.Tracker object>

A Tracker that can store information about device executions, shots, batches, intermediate results, or any additional device dependent information.

A plugin developer can store information in the tracker by:

# querying if the tracker is active
if self.tracker.active:

    # store any keyword: value pairs of information
    self.tracker.update(executions=1, shots=self._shots, results=results)

    # Calling a user-provided callback function
    self.tracker.record()
wires

The device wires.

Note that wires are optional, and the default value of None means any wires can be used. If a device has wires defined, they will only be used for certain features. This includes:

  • Validation of tapes being executed on the device

  • Defining the wires used when evaluating a state() measurement

compute_derivatives(circuits[, execution_config])

Calculate the jacobian of either a single or a batch of circuits on the device.

compute_jvp(circuits, tangents[, ...])

The jacobian vector product used in forward mode calculation of derivatives.

compute_vjp(circuits, cotangents[, ...])

The vector jacobian product used in reverse-mode differentiation. LightningQubit uses the adjoint differentiation method to compute the VJP. :param circuits: the circuit or batch of circuits :type circuits: Union[QuantumTape, Sequence[QuantumTape]] :param cotangents: Gradient-output vector. Must have shape matching the output shape of the corresponding circuit. If the circuit has a single output, cotangents may be a single number, not an iterable of numbers. :type cotangents: Tuple[Number, Tuple[Number]] :param execution_config: a datastructure with all additional information required for execution :type execution_config: ExecutionConfig.

execute(circuits[, execution_config])

Execute a circuit or a batch of circuits and turn it into results.

execute_and_compute_derivatives(circuits[, ...])

Compute the results and jacobians of circuits at the same time.

execute_and_compute_jvp(circuits, tangents)

Execute a batch of circuits and compute their jacobian vector products.

execute_and_compute_vjp(circuits, cotangents)

Calculate both the results and the vector jacobian product used in reverse-mode differentiation. :param circuits: the circuit or batch of circuits to be executed :type circuits: Union[QuantumTape, Sequence[QuantumTape]] :param cotangents: Gradient-output vector. Must have shape matching the output shape of the corresponding circuit. If the circuit has a single output, cotangents may be a single number, not an iterable of numbers. :type cotangents: Tuple[Number, Tuple[Number]] :param execution_config: a datastructure with all additional information required for execution :type execution_config: ExecutionConfig.

preprocess([execution_config])

This function defines the device transform program to be applied and an updated device configuration.

supports_derivatives([execution_config, circuit])

Check whether or not derivatives are available for a given configuration and circuit.

supports_jvp([execution_config, circuit])

Whether or not a given device defines a custom jacobian vector product.

supports_vjp([execution_config, circuit])

Whether or not this device defines a custom vector jacobian product.

compute_derivatives(circuits: Union[QuantumTape, Sequence[QuantumTape]], execution_config: ExecutionConfig = ExecutionConfig(grad_on_execution=None, use_device_gradient=None, use_device_jacobian_product=None, gradient_method=None, gradient_keyword_arguments={}, device_options={}, interface=None, derivative_order=1))[source]

Calculate the jacobian of either a single or a batch of circuits on the device.

Parameters
  • circuits (Union[QuantumTape, Sequence[QuantumTape]]) – the circuits to calculate derivatives for

  • execution_config (ExecutionConfig) – a datastructure with all additional information required for execution

Returns

The jacobian for each trainable parameter

Return type

Tuple

compute_jvp(circuits: Union[QuantumTape, Sequence[QuantumTape]], tangents: Tuple[Number], execution_config: ExecutionConfig = ExecutionConfig(grad_on_execution=None, use_device_gradient=None, use_device_jacobian_product=None, gradient_method=None, gradient_keyword_arguments={}, device_options={}, interface=None, derivative_order=1))

The jacobian vector product used in forward mode calculation of derivatives.

Parameters
  • circuits (Union[QuantumTape, Sequence[QuantumTape]]) – the circuit or batch of circuits

  • tangents (tensor-like) – Gradient vector for input parameters.

  • execution_config (ExecutionConfig) – a datastructure with all additional information required for execution

Returns

A numeric result of computing the jacobian vector product

Return type

Tuple

Definition of jvp:

If we have a function with jacobian:

\[\vec{y} = f(\vec{x}) \qquad J_{i,j} = \frac{\partial y_i}{\partial x_j}\]

The Jacobian vector product is the inner product with the derivatives of \(x\), yielding only the derivatives of the output \(y\):

\[\text{d}y_i = \Sigma_{j} J_{i,j} \text{d}x_j\]

Shape of tangents:

The tangents tuple should be the same length as circuit.get_parameters() and have a single number per parameter. If a number is zero, then the gradient with respect to that parameter does not need to be computed.

compute_vjp(circuits: Union[QuantumTape, Sequence[QuantumTape]], cotangents: Tuple[Number], execution_config: ExecutionConfig = ExecutionConfig(grad_on_execution=None, use_device_gradient=None, use_device_jacobian_product=None, gradient_method=None, gradient_keyword_arguments={}, device_options={}, interface=None, derivative_order=1))[source]

The vector jacobian product used in reverse-mode differentiation. LightningQubit uses the adjoint differentiation method to compute the VJP. :param circuits: the circuit or batch of circuits :type circuits: Union[QuantumTape, Sequence[QuantumTape]] :param cotangents: Gradient-output vector. Must have shape matching the output shape of the

corresponding circuit. If the circuit has a single output, cotangents may be a single number, not an iterable of numbers.

Parameters

execution_config (ExecutionConfig) – a datastructure with all additional information required for execution

Returns

A numeric result of computing the vector jacobian product

Return type

tensor-like

Definition of vjp: If we have a function with jacobian: .. math:

\vec{y} = f(\vec{x}) \qquad J_{i,j} = \frac{\partial y_i}{\partial x_j}

The vector jacobian product is the inner product of the derivatives of the output y with the Jacobian matrix. The derivatives of the output vector are sometimes called the cotangents. .. math:

\text{d}x_i = \Sigma_{i} \text{d}y_i J_{i,j}

Shape of cotangents: The value provided to cotangents should match the output of execute(). For computing the full Jacobian, the cotangents can be batched to vectorize the computation. In this case, the cotangents can have the following shapes. batch_size below refers to the number of entries in the Jacobian: * For a state measurement, the cotangents must have shape (batch_size, 2 ** n_wires) * For n expectation values, the cotangents must have shape (n, batch_size). If n = 1,

then the shape must be (batch_size,).

execute(circuits: Union[QuantumTape, Sequence[QuantumTape]], execution_config: ExecutionConfig = ExecutionConfig(grad_on_execution=None, use_device_gradient=None, use_device_jacobian_product=None, gradient_method=None, gradient_keyword_arguments={}, device_options={}, interface=None, derivative_order=1)) Union[Result, Tuple[Result]][source]

Execute a circuit or a batch of circuits and turn it into results.

Parameters
  • circuits (Union[QuantumTape, Sequence[QuantumTape]]) – the quantum circuits to be executed

  • execution_config (ExecutionConfig) – a datastructure with additional information required for execution

Returns

A numeric result of the computation.

Return type

TensorLike, tuple[TensorLike], tuple[tuple[TensorLike]]

execute_and_compute_derivatives(circuits: Union[QuantumTape, Sequence[QuantumTape]], execution_config: ExecutionConfig = ExecutionConfig(grad_on_execution=None, use_device_gradient=None, use_device_jacobian_product=None, gradient_method=None, gradient_keyword_arguments={}, device_options={}, interface=None, derivative_order=1))[source]

Compute the results and jacobians of circuits at the same time.

Parameters
  • circuits (Union[QuantumTape, Sequence[QuantumTape]]) – the circuits or batch of circuits

  • execution_config (ExecutionConfig) – a datastructure with all additional information required for execution

Returns

A numeric result of the computation and the gradient.

Return type

tuple

execute_and_compute_jvp(circuits: Union[QuantumTape, Sequence[QuantumTape]], tangents: Tuple[Number], execution_config: ExecutionConfig = ExecutionConfig(grad_on_execution=None, use_device_gradient=None, use_device_jacobian_product=None, gradient_method=None, gradient_keyword_arguments={}, device_options={}, interface=None, derivative_order=1))

Execute a batch of circuits and compute their jacobian vector products.

Parameters
  • circuits (Union[QuantumTape, Sequence[QuantumTape]]) – circuit or batch of circuits

  • tangents (tensor-like) – Gradient vector for input parameters.

  • execution_config (ExecutionConfig) – a datastructure with all additional information required for execution

Returns

A numeric result of execution and of computing the jacobian vector product

Return type

Tuple, Tuple

See also

execute() and compute_jvp()

execute_and_compute_vjp(circuits: Union[QuantumTape, Sequence[QuantumTape]], cotangents: Tuple[Number], execution_config: ExecutionConfig = ExecutionConfig(grad_on_execution=None, use_device_gradient=None, use_device_jacobian_product=None, gradient_method=None, gradient_keyword_arguments={}, device_options={}, interface=None, derivative_order=1))[source]

Calculate both the results and the vector jacobian product used in reverse-mode differentiation. :param circuits: the circuit or batch of circuits to be executed :type circuits: Union[QuantumTape, Sequence[QuantumTape]] :param cotangents: Gradient-output vector. Must have shape matching the output shape of the

corresponding circuit. If the circuit has a single output, cotangents may be a single number, not an iterable of numbers.

Parameters

execution_config (ExecutionConfig) – a datastructure with all additional information required for execution

Returns

the result of executing the scripts and the numeric result of computing the vector jacobian product

Return type

Tuple, Tuple

preprocess(execution_config: ExecutionConfig = ExecutionConfig(grad_on_execution=None, use_device_gradient=None, use_device_jacobian_product=None, gradient_method=None, gradient_keyword_arguments={}, device_options={}, interface=None, derivative_order=1))[source]

This function defines the device transform program to be applied and an updated device configuration.

Parameters

execution_config (Union[ExecutionConfig, Sequence[ExecutionConfig]]) – A data structure describing the parameters needed to fully describe the execution.

Returns

A transform program that when called returns QuantumTape’s that the device can natively execute as well as a postprocessing function to be called after execution, and a configuration with unset specifications filled in.

Return type

TransformProgram, ExecutionConfig

This device:

  • Supports any qubit operations that provide a matrix

  • Currently does not support finite shots

  • Currently does not intrinsically support parameter broadcasting

supports_derivatives(execution_config: Optional[ExecutionConfig] = None, circuit: Optional[QuantumTape] = None) bool[source]

Check whether or not derivatives are available for a given configuration and circuit.

LightningQubit supports adjoint differentiation with analytic results.

Parameters
  • execution_config (ExecutionConfig) – The configuration of the desired derivative calculation

  • circuit (QuantumTape) – An optional circuit to check derivatives support for.

Returns

Whether or not a derivative can be calculated provided the given information

Return type

Bool

supports_jvp(execution_config: Optional[ExecutionConfig] = None, circuit: Optional[QuantumTape] = None) bool

Whether or not a given device defines a custom jacobian vector product.

Parameters
  • execution_config (ExecutionConfig) – A description of the hyperparameters for the desired computation.

  • circuit (None, QuantumTape) – A specific circuit to check differentation for.

Default behaviour assumes this to be True if compute_jvp() is overridden.

supports_vjp(execution_config: Optional[ExecutionConfig] = None, circuit: Optional[QuantumTape] = None) bool[source]

Whether or not this device defines a custom vector jacobian product. LightningQubit supports adjoint differentiation with analytic results. :param execution_config: The configuration of the desired derivative calculation :type execution_config: ExecutionConfig :param circuit: An optional circuit to check derivatives support for. :type circuit: QuantumTape

Returns

Whether or not a derivative can be calculated provided the given information

Return type

Bool