RandomLayer#

class torchquantum.layer.RandomLayer(wires, n_ops=None, n_params=None, op_ratios=None, op_types=(<class 'torchquantum.operator.standard_gates.rx.RX'>, <class 'torchquantum.operator.standard_gates.ry.RY'>, <class 'torchquantum.operator.standard_gates.rz.RZ'>, <class 'torchquantum.operator.standard_gates.paulix.CNOT'>), seed=None, qiskit_compatible=False)[source]#

Bases: QuantumModule

Quantum module that represents a random layer of quantum operations applied to specified wires.

Parameters:
  • wires (int or Iterable[int]) – Indices of the wires the operations are applied to.

  • n_ops (int) – Number of random operations in the layer.

  • n_params (int) – Number of parameters for each random operation.

  • op_ratios (list or float) – Ratios determining the relative frequencies of different operation types.

  • op_types (tuple or tq.Operator) – Types of random operations to be included in the layer.

  • seed (int) – Seed for random number generation.

  • qiskit_compatible (bool) – Flag indicating whether the layer should be compatible with Qiskit.

n_ops#

Number of random operations in the layer.

Type:

int

n_params#

Number of parameters for each random operation.

Type:

int

wires#

Indices of the wires the operations are applied to.

Type:

list

n_wires#

Number of wires.

Type:

int

op_types#

Types of random operations included in the layer.

Type:

list

op_ratios#

Ratios determining the relative frequencies of different operation types.

Type:

numpy.array

seed#

Seed for random number generation.

Type:

int

op_list#

List of random operations in the layer.

Type:

tq.QuantumModuleList

__init__(wires, n_ops=None, n_params=None, op_ratios=None, op_types=(<class 'torchquantum.operator.standard_gates.rx.RX'>, <class 'torchquantum.operator.standard_gates.ry.RY'>, <class 'torchquantum.operator.standard_gates.rz.RZ'>, <class 'torchquantum.operator.standard_gates.paulix.CNOT'>), seed=None, qiskit_compatible=False)[source]#

Initialize the QuantumModule.

Returns:

None.

Examples

>>> qmodule = QuantumModule()

Methods

build_random_layer()[source]#
forward(q_device: QuantumDevice)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

rebuild_random_layer_from_op_list(n_ops_in, wires_in, op_list_in)[source]#

Rebuilds a random layer from the given operation list. This method is used for loading a random layer from a checkpoint.

Parameters:
  • n_ops_in (int) – Number of operations in the layer.

  • wires_in (list) – Indices of the wires the operations are applied to.

  • op_list_in (list) – List of operations in the layer.

Attributes

training: bool#