quairkit.ansatz.container

The source file of the Sequential class.

class quairkit.ansatz.container.OperatorList(num_systems, system_dim, physical_idx)

Sequential container for QuAIRKit operators.

Parameters:
num_systems : int | None

number of systems in the operator list.

system_dim : List[int] | int

dimension of systems of this list. Can be a list of system dimensions or an int representing the dimension of all systems.

physical_idx : List[int] | None

physical indices of systems.

property num_qubits : int

Number of qubits in this circuit.

property num_qutrits : int

Number of qutrits in this circuit.

property system_dim : list[int]

Dimension of systems in this circuit.

property equal_dim : bool

Whether the systems in this circuit have the same dimension.

property system_idx : list[int]

List of physical indices of systems.

add_systems(num_new_systems, new_system_dim=None, new_physical_idx=None)

Add new systems to the list.

Parameters:
num_new_systems : int

number of new systems to be added.

new_system_dim : int | List[int] | None

dimension of new systems. Defaults to be the same as other systems.

new_physical_idx : List[int] | None

physical indices of new systems. Defaults to start from the largest index.

sort()

Sort the systems in the circuit by their physical indices.

property num_systems : int

Number of logical systems.

register_idx(operator_idx, num_acted_system)

Update sequential according to input operator index information, or report error.

Parameters:
operator_idx : Iterable[int] | int | str | None

input system indices of the operator. None means acting on all systems.

num_acted_system : int

number of systems that one operator acts on.

Returns:

the formatted system indices.

Return type:

List[List[int]]

append(op)

Appends an operator or an operator sub-list to the end.

Parameters:
op : Operator | OperatorList

module to append

extend(sequential)

Extend the list with another sequential

Parameters:
sequential : OperatorList

a sequential of operators to be extended

Returns:

Concatenation of two quantum operator sequential

Return type:

OperatorList

operators()

Yield all operators in this list.

Returns:

A generator of operators

Return type:

Generator[Operator, None, None]

property operator_history : list[OperatorInfoType | list[OperatorInfoType]]

Return the operator history of this Sequential

property param : Tensor

Flattened parameters in this list.

property grad : ndarray

Gradients with respect to the flattened parameters.

update_param(theta, idx=None)

Replace parameters of all/one layer(s) by theta.

Parameters:
theta : Tensor | ndarray | float

New parameters

idx : int | None

Index of replacement. Defaults to None, referring to all layers.

transfer_static()

set stop_gradient of all parameters of the list as True

randomize_param(arg0=0, arg1=6.283185307179586, method='uniform')

Randomize parameters of the list based on the initializer. Current we only support Uniform and Normal initializer.

Parameters:
arg0 : float

first argument of the initializer. Defaults to 0.

arg1 : float

first argument of the initializer. Defaults to 2 pi.

method : str

The sampling method. Defaults to ‘uniform’.

forward(state=None)

Passing a physical input state.

Parameters:
state : State | None

initial state. Defaults to zero state.

Returns:

output quantum state

Return type:

State

property matrix : Tensor

Get the unitary matrix form of the operator list.

unitary_matrix()

Get the unitary matrix form of the circuit.

Returns:

Unitary matrix form of the circuit.

Return type:

Tensor

property depth : int

Depth of gate sequences.

Returns:

depth of this circuit

Note

The measurement is omitted, and all gates are assumed to have depth 1. See Niel’s answer in the [StackExchange](https://quantumcomputing.stackexchange.com/a/5772).

property qasm : str

String representation of the circuit in OpenQASM-like format.

TODO: remove n

class quairkit.ansatz.container.Layer(physical_idx, depth, name='Layer', system_dim=2)

Base class for built-in trainable quantum circuit ansatz.

Parameters:
physical_idx : List[int]

Physical indices of the systems on which this layer is applied.

depth : int

Depth of the layer.

name : str

Name of the layer. Defaults to ‘Layer’.

system_dim : int | List[int]

Dimension of the systems. Defaults to be qubit-systems.

Note

A Circuit instance needs to extend this Layer instance to be used in a circuit.

get_latex_name(style='standard')

Return the LaTeX name of the layer.

Parameters:
style : str

the style of the plot, can be ‘standard’, ‘compact’ or ‘detailed’. Defaults to standard.

plot(style='standard', decimal=2, dpi=300, print_code=False, show_plot=True)

Plot the circuit layer using LaTeX

Parameters:
style : str

the style of the plot, can be ‘standard’, ‘compact’ or ‘detailed’. Defaults to standard.

decimal : int

number of decimal places to display. Defaults to 2.

dpi : int

dots per inches of plot image. Defaults to 300.

print_code : bool

whether print the LaTeX code of the circuit, default to True.

show_plot : bool

whether show the plotted circuit, default to True.