matchcake.operations package¶
Subpackages¶
- matchcake.operations.single_particle_transition_matrices package
- Submodules
- matchcake.operations.single_particle_transition_matrices.single_particle_transition_matrix module
SingleParticleTransitionMatrixOperationSingleParticleTransitionMatrixOperation.ALLOWED_ANGLESSingleParticleTransitionMatrixOperation.DEFAULT_CHECK_ANGLESSingleParticleTransitionMatrixOperation.DEFAULT_CHECK_MATRIXSingleParticleTransitionMatrixOperation.DEFAULT_CLIP_ANGLESSingleParticleTransitionMatrixOperation.DEFAULT_NORMALIZESingleParticleTransitionMatrixOperation.EQUAL_ALLOWED_ANGLESSingleParticleTransitionMatrixOperation.__init__()SingleParticleTransitionMatrixOperation.adjoint()SingleParticleTransitionMatrixOperation.casting_prioritiesSingleParticleTransitionMatrixOperation.check_angles()SingleParticleTransitionMatrixOperation.check_is_in_so4()SingleParticleTransitionMatrixOperation.check_is_unitary()SingleParticleTransitionMatrixOperation.clip_angles()SingleParticleTransitionMatrixOperation.clip_to_allowed_angles()SingleParticleTransitionMatrixOperation.cs_wiresSingleParticleTransitionMatrixOperation.from_operation()SingleParticleTransitionMatrixOperation.from_operations()SingleParticleTransitionMatrixOperation.generatorSingleParticleTransitionMatrixOperation.grad_methodSingleParticleTransitionMatrixOperation.grad_recipeSingleParticleTransitionMatrixOperation.matrix()SingleParticleTransitionMatrixOperation.num_paramsSingleParticleTransitionMatrixOperation.num_wiresSingleParticleTransitionMatrixOperation.pad()SingleParticleTransitionMatrixOperation.par_domainSingleParticleTransitionMatrixOperation.random()SingleParticleTransitionMatrixOperation.random_params()SingleParticleTransitionMatrixOperation.real()SingleParticleTransitionMatrixOperation.sorted_wiresSingleParticleTransitionMatrixOperation.to_cuda()SingleParticleTransitionMatrixOperation.to_torch()
- matchcake.operations.single_particle_transition_matrices.sptm_angle_embedding module
- matchcake.operations.single_particle_transition_matrices.sptm_block_diag_angle_embedding module
- matchcake.operations.single_particle_transition_matrices.sptm_comp_hh module
- matchcake.operations.single_particle_transition_matrices.sptm_comp_rxrx module
- matchcake.operations.single_particle_transition_matrices.sptm_comp_ryry module
- matchcake.operations.single_particle_transition_matrices.sptm_comp_rzrz module
- matchcake.operations.single_particle_transition_matrices.sptm_diag_embedding module
- matchcake.operations.single_particle_transition_matrices.sptm_fermionic_superposition module
- matchcake.operations.single_particle_transition_matrices.sptm_fswap module
- matchcake.operations.single_particle_transition_matrices.sptm_fswap_hh module
- matchcake.operations.single_particle_transition_matrices.sptm_identity module
- Module contents
Submodules¶
matchcake.operations.angle_embedding module¶
- class matchcake.operations.angle_embedding.MAngleEmbedding(features, wires, rotations='X', id=None, **kwargs)¶
Bases:
Operation- __init__(features, wires, rotations='X', id=None, **kwargs)¶
Construct a new Matchgate AngleEmbedding operation.
- Note:
It is better to have one more wire than the number of features because every gate acts on two wires.
- Parameters:
features – The features to embed.
wires – The wires to embed the features on.
id – The id of the operation.
- Keyword Arguments:
contract_rots – If True, contract the rotations. Default is False.
- static compute_decomposition(*params, wires=None, **hyperparameters)¶
Representation of the operator as a product of other operators (static method).
\[O = O_1 O_2 \dots O_n.\]Note
Operations making up the decomposition should be queued within the
compute_decompositionmethod.See also
decomposition().- Parameters:
*params (list) – trainable parameters of the operator, as stored in the
parametersattributewires (Iterable[Any], Wires) – wires that the operator acts on
**hyperparams (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparametersattribute
- Returns:
decomposition of the operator
- Return type:
list[Operator]
- grad_method = None¶
- property ndim_params¶
Number of dimensions per trainable parameter of the operator.
By default, this property returns the numbers of dimensions of the parameters used for the operator creation. If the parameter sizes for an operator subclass are fixed, this property can be overwritten to return the fixed value.
- Returns:
Number of dimensions for each trainable parameter.
- Return type:
tuple
- property num_params¶
Number of trainable parameters that the operator depends on.
By default, this property returns as many parameters as were used for the operator creation. If the number of parameters for an operator subclass is fixed, this property can be overwritten to return the fixed value.
- Returns:
number of parameters
- Return type:
int
- num_wires: int | WiresEnum = -1¶
Number of wires the operator acts on.
- static pad_params(params)¶
If the number of parameters is odd, pad the parameters with zero to make it even.
- Parameters:
params – The parameters to pad.
- Returns:
The padded parameters.
- class matchcake.operations.angle_embedding.MAngleEmbeddings(features, wires, rotations='X', id=None)¶
Bases:
Operation- __init__(features, wires, rotations='X', id=None)¶
Construct a new Matchgate AngleEmbedding operation.
- Note:
It is better to have one more wire than the number of features because every gate acts on two wires.
- Parameters:
features – The features to embed.
wires – The wires to embed the features on.
id – The id of the operation.
- static compute_decomposition(*params, wires=None, **hyperparameters)¶
Representation of the operator as a product of other operators (static method).
\[O = O_1 O_2 \dots O_n.\]Note
Operations making up the decomposition should be queued within the
compute_decompositionmethod.See also
decomposition().- Parameters:
*params (list) – trainable parameters of the operator, as stored in the
parametersattributewires (Iterable[Any], Wires) – wires that the operator acts on
**hyperparams (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparametersattribute
- Returns:
decomposition of the operator
- Return type:
list[Operator]
- grad_method = None¶
- property ndim_params¶
Number of dimensions per trainable parameter of the operator.
By default, this property returns the numbers of dimensions of the parameters used for the operator creation. If the parameter sizes for an operator subclass are fixed, this property can be overwritten to return the fixed value.
- Returns:
Number of dimensions for each trainable parameter.
- Return type:
tuple
- property num_params¶
Number of trainable parameters that the operator depends on.
By default, this property returns as many parameters as were used for the operator creation. If the number of parameters for an operator subclass is fixed, this property can be overwritten to return the fixed value.
- Returns:
number of parameters
- Return type:
int
- num_wires: int | WiresEnum = -1¶
Number of wires the operator acts on.
- static pad_params(params)¶
If the number of parameters is odd, pad the parameters with zero to make it even.
- Parameters:
params – The parameters to pad.
- Returns:
The padded parameters.
matchcake.operations.comp_hh module¶
- class matchcake.operations.comp_hh.CompHH(matrix: TensorLike | Tensor | Number | MatchgateParams, wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
Bases:
MatchgateOperationRepresents a matchgate composition of two hadamard gates
\[U = M(H, H)\]where \(M\) is a matchgate, \(H\) is the hadamard gate. It’s also called the fermionic hadamard.
- label(decimals=None, base_label=None, cache=None)¶
A customizable string representation of the operator.
- Parameters:
decimals=None (int) – If
None, no parameters are included. Else, specifies how to round the parameters.base_label=None (str) – overwrite the non-parameter component of the label
cache=None (dict) – dictionary that carries information between label calls in the same drawing
- Returns:
label to use in drawings
- Return type:
str
Example:
>>> op = qml.RX(1.23456, wires=0) >>> op.label() "RX" >>> op.label(base_label="my_label") "my_label" >>> op = qml.RX(1.23456, wires=0, id="test_data") >>> op.label() "RX("test_data")" >>> op.label(decimals=2) "RX\n(1.23,"test_data")" >>> op.label(base_label="my_label") "my_label("test_data")" >>> op.label(decimals=2, base_label="my_label") "my_label\n(1.23,"test_data")"
If the operation has a matrix-valued parameter and a cache dictionary is provided, unique matrices will be cached in the
'matrices'key list. The label will contain the index of the matrix in the'matrices'list.>>> op2 = qml.QubitUnitary(np.eye(2), wires=0) >>> cache = {'matrices': []} >>> op2.label(cache=cache) 'U(M0)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True)] >>> op3 = qml.QubitUnitary(np.eye(4), wires=(0,1)) >>> op3.label(cache=cache) 'U(M1)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True), tensor([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]], requires_grad=True)]
- num_params = 0¶
- num_wires: int | WiresEnum = 2¶
Number of wires the operator acts on.
- classmethod random(wires: Wires, batch_size=None, **kwargs)¶
Generate a random MatchgateOperation instance.
This class method creates a new instance of MatchgateOperation with randomly initialized parameters. The generated object can be used for testing, experimentation, or random state initialization.
The random values for the parameters can be controlled using the provided seed. Additional configuration can be done using various optional parameters such as the batch size, data type, device, and any implementation-specific keyword arguments.
- Parameters:
wires (Wires) – Wires that the operation acts on. Can be a sequence or scalar.
batch_size – Optional size of generated random batches. If None, a single gate is returned.
dtype – Torch data type used for the random parameters. Defaults to torch.complex128.
device – Optional Torch device on which the parameters will be created. If None, parameters are created on the default device.
seed – Optional integer seed for controlling randomness. If None, the seed is not fixed.
kwargs – Additional keyword arguments for specific configuration or compatibility.
- Returns:
A new MatchgateOperation instance with randomly initialized parameters.
- Return type:
- to_sptm_operation() SingleParticleTransitionMatrixOperation¶
matchcake.operations.comp_paulis module¶
- class matchcake.operations.comp_paulis.CompPauli(*args, **kwargs)¶
Bases:
MatchgateOperationRepresents a matchgate composition of paulis gates
\[U = M(P_0, P_1)\]where \(M\) is a matchgate, \(P_0\) and \(P_1\) are the paulis.
- __init__(*args, **kwargs)¶
- get_implicit_parameters()¶
- label(decimals=None, base_label=None, cache=None)¶
A customizable string representation of the operator.
- Parameters:
decimals=None (int) – If
None, no parameters are included. Else, specifies how to round the parameters.base_label=None (str) – overwrite the non-parameter component of the label
cache=None (dict) – dictionary that carries information between label calls in the same drawing
- Returns:
label to use in drawings
- Return type:
str
Example:
>>> op = qml.RX(1.23456, wires=0) >>> op.label() "RX" >>> op.label(base_label="my_label") "my_label" >>> op = qml.RX(1.23456, wires=0, id="test_data") >>> op.label() "RX("test_data")" >>> op.label(decimals=2) "RX\n(1.23,"test_data")" >>> op.label(base_label="my_label") "my_label("test_data")" >>> op.label(decimals=2, base_label="my_label") "my_label\n(1.23,"test_data")"
If the operation has a matrix-valued parameter and a cache dictionary is provided, unique matrices will be cached in the
'matrices'key list. The label will contain the index of the matrix in the'matrices'list.>>> op2 = qml.QubitUnitary(np.eye(2), wires=0) >>> cache = {'matrices': []} >>> op2.label(cache=cache) 'U(M0)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True)] >>> op3 = qml.QubitUnitary(np.eye(4), wires=(0,1)) >>> op3.label(cache=cache) 'U(M1)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True), tensor([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]], requires_grad=True)]
- num_params = 0¶
- num_wires: int | WiresEnum = 2¶
Number of wires the operator acts on.
- classmethod random(wires: Wires, batch_size=None, **kwargs)¶
Generate a random MatchgateOperation instance.
This class method creates a new instance of MatchgateOperation with randomly initialized parameters. The generated object can be used for testing, experimentation, or random state initialization.
The random values for the parameters can be controlled using the provided seed. Additional configuration can be done using various optional parameters such as the batch size, data type, device, and any implementation-specific keyword arguments.
- Parameters:
wires (Wires) – Wires that the operation acts on. Can be a sequence or scalar.
batch_size – Optional size of generated random batches. If None, a single gate is returned.
dtype – Torch data type used for the random parameters. Defaults to torch.complex128.
device – Optional Torch device on which the parameters will be created. If None, parameters are created on the default device.
seed – Optional integer seed for controlling randomness. If None, the seed is not fixed.
kwargs – Additional keyword arguments for specific configuration or compatibility.
- Returns:
A new MatchgateOperation instance with randomly initialized parameters.
- Return type:
matchcake.operations.comp_rotations module¶
- class matchcake.operations.comp_rotations.CompRotation(params, directions: Sequence[Literal['X', 'Y', 'Z']], wires=None, id=None, **kwargs)¶
Bases:
MatchgateOperationComposition of rotations
\[U = M(R_{P0}(\theta), R_{P1}(\phi))\]where \(M\) is a matchgate, \(P0\) and \(P1\) are the paulis.
- __init__(params, directions: Sequence[Literal['X', 'Y', 'Z']], wires=None, id=None, **kwargs)¶
- get_implicit_parameters()¶
- label(decimals=None, base_label=None, cache=None)¶
A customizable string representation of the operator.
- Parameters:
decimals=None (int) – If
None, no parameters are included. Else, specifies how to round the parameters.base_label=None (str) – overwrite the non-parameter component of the label
cache=None (dict) – dictionary that carries information between label calls in the same drawing
- Returns:
label to use in drawings
- Return type:
str
Example:
>>> op = qml.RX(1.23456, wires=0) >>> op.label() "RX" >>> op.label(decimals=2) "RX\n(1.23)" >>> op.label(base_label="my_label") "my_label" >>> op.label(decimals=2, base_label="my_label") "my_label\n(1.23)"
If the operation has a matrix-valued parameter and a cache dictionary is provided, unique matrices will be cached in the
'matrices'key list. The label will contain the index of the matrix in the'matrices'list.>>> op2 = qml.QubitUnitary(np.eye(2), wires=0) >>> cache = {'matrices': []} >>> op2.label(cache=cache) 'U(M0)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True)] >>> op3 = qml.QubitUnitary(np.eye(4), wires=(0,1)) >>> op3.label(cache=cache) 'U(M1)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True), tensor([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]], requires_grad=True)]
- num_params = 1¶
- num_wires: int | WiresEnum = 2¶
Number of wires the operator acts on.
- classmethod random(wires: Wires, *, batch_size: int | None = None, dtype: dtype = torch.complex128, device: device | None = None, seed: int | None = None, **kwargs) CompRotation¶
Generate a random MatchgateOperation instance.
This class method creates a new instance of MatchgateOperation with randomly initialized parameters. The generated object can be used for testing, experimentation, or random state initialization.
The random values for the parameters can be controlled using the provided seed. Additional configuration can be done using various optional parameters such as the batch size, data type, device, and any implementation-specific keyword arguments.
- Parameters:
wires (Wires) – Wires that the operation acts on. Can be a sequence or scalar.
batch_size – Optional size of generated random batches. If None, a single gate is returned.
dtype – Torch data type used for the random parameters. Defaults to torch.complex128.
device – Optional Torch device on which the parameters will be created. If None, parameters are created on the default device.
seed – Optional integer seed for controlling randomness. If None, the seed is not fixed.
kwargs – Additional keyword arguments for specific configuration or compatibility.
- Returns:
A new MatchgateOperation instance with randomly initialized parameters.
- Return type:
- classmethod random_params(batch_size=None, **kwargs)¶
Generates a tensor of random parameters. This method allows creating a tensor with random elements drawn from uniform (0, 1] for the two first elements and drawn from a uniform distribution [0, \(2\pi\)). If
batch_sizeis None, a single parameter set is returned instead of a batch.- Parameters:
batch_size (Optional[int]) – The number of parameter sets to generate. If None, only a single parameter set is generated.
dtype – The data type of the output tensor. Defaults to torch.float64.
device – The device where the tensor will be allocated. Defaults to the current default device if not specified.
seed (Optional[int]) – A manual seed for reproducible random generation. If None, the random generator is not seeded manually.
kwargs – Additional keyword arguments for extended configuration.
- Returns:
A tensor containing the generated random parameters. If
batch_sizeis specified, the shape will be(batch_size, 7). Otherwise, the returned tensor has shape(7,).- Return type:
torch.Tensor
- class matchcake.operations.comp_rotations.CompRxRx(params, wires=None, id=None, **kwargs)¶
Bases:
CompRotationComposition of rotation XX which mean a matchgate
\[U = M(R_X(\theta), R_X(\phi))\]- __init__(params, wires=None, id=None, **kwargs)¶
- to_sptm_operation() SingleParticleTransitionMatrixOperation¶
- class matchcake.operations.comp_rotations.CompRyRy(params, wires=None, id=None, **kwargs)¶
Bases:
CompRotationComposition of rotation YY which mean a matchgate
\[U = M(R_Y(\theta), R_Y(\phi))\]- __init__(params, wires=None, id=None, **kwargs)¶
- to_sptm_operation() SingleParticleTransitionMatrixOperation¶
- class matchcake.operations.comp_rotations.CompRzRz(params, wires=None, id=None, **kwargs)¶
Bases:
CompRotationComposition of rotation ZZ which mean a matchgate
\[U = M(R_Z(\theta), R_Z(\phi))\]- __init__(params, wires=None, id=None, **kwargs)¶
- to_sptm_operation() SingleParticleTransitionMatrixOperation¶
matchcake.operations.fermionic_controlled_z module¶
- matchcake.operations.fermionic_controlled_z.FermionicControlledZ¶
alias of
FermionicControlledZ
- class matchcake.operations.fermionic_controlled_z.fCZ(*params: TensorLike, wires: Wires | Iterable[Hashable] | Hashable | None = None, id: str | None = None)¶
Bases:
Operation- static compute_decomposition(*params, wires=None, **hyperparameters)¶
Representation of the operator as a product of other operators (static method).
\[O = O_1 O_2 \dots O_n.\]Note
Operations making up the decomposition should be queued within the
compute_decompositionmethod.See also
decomposition().- Parameters:
*params (list) – trainable parameters of the operator, as stored in the
parametersattributewires (Iterable[Any], Wires) – wires that the operator acts on
**hyperparams (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparametersattribute
- Returns:
decomposition of the operator
- Return type:
list[Operator]
- label(decimals=None, base_label=None, cache=None)¶
A customizable string representation of the operator.
- Parameters:
decimals=None (int) – If
None, no parameters are included. Else, specifies how to round the parameters.base_label=None (str) – overwrite the non-parameter component of the label
cache=None (dict) – dictionary that carries information between label calls in the same drawing
- Returns:
label to use in drawings
- Return type:
str
Example:
>>> op = qml.RX(1.23456, wires=0) >>> op.label() "RX" >>> op.label(base_label="my_label") "my_label" >>> op = qml.RX(1.23456, wires=0, id="test_data") >>> op.label() "RX("test_data")" >>> op.label(decimals=2) "RX\n(1.23,"test_data")" >>> op.label(base_label="my_label") "my_label("test_data")" >>> op.label(decimals=2, base_label="my_label") "my_label\n(1.23,"test_data")"
If the operation has a matrix-valued parameter and a cache dictionary is provided, unique matrices will be cached in the
'matrices'key list. The label will contain the index of the matrix in the'matrices'list.>>> op2 = qml.QubitUnitary(np.eye(2), wires=0) >>> cache = {'matrices': []} >>> op2.label(cache=cache) 'U(M0)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True)] >>> op3 = qml.QubitUnitary(np.eye(4), wires=(0,1)) >>> op3.label(cache=cache) 'U(M1)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True), tensor([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]], requires_grad=True)]
- num_params = 0¶
- num_wires: int | WiresEnum = 2¶
Number of wires the operator acts on.
matchcake.operations.fermionic_superposition module¶
- class matchcake.operations.fermionic_superposition.FermionicSuperposition(*params: TensorLike, wires: Wires | Iterable[Hashable] | Hashable | None = None, id: str | None = None)¶
Bases:
OperationRepresents a Fermionic Superposition operation.
This class implements a quantum operation where, when applied to the vacuum state, each even mode will be in an equal superposition. It allows for operations over arbitrary numbers of qubits and is specifically designed to work with even numbers of wires. The operation is configured with a set of wires and optional parameters. Additionally, it supports features such as decomposition into fundamental gates.
- static compute_decomposition(*params, wires=None, **hyperparameters)¶
Representation of the operator as a product of other operators (static method).
\[O = O_1 O_2 \dots O_n.\]Note
Operations making up the decomposition should be queued within the
compute_decompositionmethod.See also
decomposition().- Parameters:
*params (list) – trainable parameters of the operator, as stored in the
parametersattributewires (Iterable[Any], Wires) – wires that the operator acts on
**hyperparams (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparametersattribute
- Returns:
decomposition of the operator
- Return type:
list[Operator]
- grad_method = None¶
- property num_params¶
Number of trainable parameters that the operator depends on.
By default, this property returns as many parameters as were used for the operator creation. If the number of parameters for an operator subclass is fixed, this property can be overwritten to return the fixed value.
- Returns:
number of parameters
- Return type:
int
- num_wires: int | WiresEnum = -1¶
Number of wires the operator acts on.
- classmethod random(wires: Wires, **kwargs)¶
- to_sptm_operation() SptmFermionicSuperposition¶
matchcake.operations.fermionic_swap module¶
- class matchcake.operations.fermionic_swap.fSWAP(wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
Bases:
MatchgateOperationRepresents a specific quantum operation that is defined using the composition of the Pauli-Z and Pauli-X matrices. This operation is usually called the fSWAP which is the SWAP gate with an additional phase.
\[\begin{split}U = M(Z, X) \\ = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix}\end{split}\]- __init__(wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
- classmethod random(wires: Wires, batch_size=None, **kwargs)¶
Generate a random MatchgateOperation instance.
This class method creates a new instance of MatchgateOperation with randomly initialized parameters. The generated object can be used for testing, experimentation, or random state initialization.
The random values for the parameters can be controlled using the provided seed. Additional configuration can be done using various optional parameters such as the batch size, data type, device, and any implementation-specific keyword arguments.
- Parameters:
wires (Wires) – Wires that the operation acts on. Can be a sequence or scalar.
batch_size – Optional size of generated random batches. If None, a single gate is returned.
dtype – Torch data type used for the random parameters. Defaults to torch.complex128.
device – Optional Torch device on which the parameters will be created. If None, parameters are created on the default device.
seed – Optional integer seed for controlling randomness. If None, the seed is not fixed.
kwargs – Additional keyword arguments for specific configuration or compatibility.
- Returns:
A new MatchgateOperation instance with randomly initialized parameters.
- Return type:
- to_sptm_operation() SingleParticleTransitionMatrixOperation¶
- matchcake.operations.fermionic_swap.fswap_chain(wires, **kwargs)¶
Generate and return a list of operations resulting from the fswap_chain_gen function.
Operations are dynamically generated based on the provided wires and the additional keyword arguments. This function serves as a convenient way to collect and group all the generated operations into a list and return it.
- Parameters:
wires (Iterable) – A list or similar iterable specifying the wires to be affected.
kwargs – Additional keyword arguments to configure the behaviour of the operation generation.
- Returns:
A list of operations generated based on the input wires and keyword arguments.
- Return type:
List
- matchcake.operations.fermionic_swap.fswap_chain_gen(wires, **kwargs)¶
Generate a sequence of fSWAP operations for a given range of wires.
The fswap_chain_gen function creates a generator that yields fSWAP operations. The direction of generation is determined by the order of the wires in the input list. If the first wire is greater than the second, the function generates operations in ascending order of wires; otherwise, it generates operations in descending order.
- Parameters:
wires – A list or tuple containing two integers that specify the range of wires to operate on. The range is determined by the smaller and larger of the two integers.
kwargs – Additional keyword arguments to be passed to the fSWAP operation.
- Returns:
A generator that yields fSWAP operations for the specified range and direction of wires.
matchcake.operations.matchgate_identity module¶
- class matchcake.operations.matchgate_identity.MatchgateIdentity(wires=None, dtype: dtype = torch.complex128, device: device | None = None, **kwargs)¶
Bases:
MatchgateOperation- __init__(wires=None, dtype: dtype = torch.complex128, device: device | None = None, **kwargs)¶
matchcake.operations.matchgate_operation module¶
- class matchcake.operations.matchgate_operation.MatchgateOperation(matrix: TensorLike | Tensor | Number | MatchgateParams, wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
Bases:
OperationA matchgate is a matrix of the form
\[\begin{split}\begin{pmatrix} a & 0 & 0 & b \\ 0 & w & x & 0 \\ 0 & y & z & 0 \\ c & 0 & 0 & d \end{pmatrix}\end{split}\]where \(a, b, c, d, w, x, y, z \in \mathbb{C}\). The matrix M can be decomposed as
\[\begin{split}A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}\end{split}\]and
\[\begin{split}W = \begin{pmatrix} w & x \\ y & z \end{pmatrix}\end{split}\]The matchgate is a unitary matrix if and only if the following conditions are satisfied:
\[M^\dagger M = \mathbb{I} \quad \text{and} \quad MM^\dagger = \mathbb{I}\]where \(\mathbb{I}\) is the identity matrix and \(M^\dagger\) is the conjugate transpose of \(M\), and the following condition is satisfied:
\[\det(A) = \det(W)\]- __init__(matrix: TensorLike | Tensor | Number | MatchgateParams, wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
- property a: Tensor¶
- adjoint()¶
Create an operation that is the adjoint of this one.
Adjointed operations are the conjugated and transposed version of the original operation. Adjointed ops are equivalent to the inverted operation for unitary gates.
- Returns:
The adjointed operation.
- property b: Tensor¶
- property batch_size: int | None¶
Batch size of the operator if it is used with broadcasted parameters.
The
batch_sizeis determined based onndim_paramsand the provided parameters for the operator. If (some of) the latter have an additional dimension, and this dimension has the same size for all parameters, its size is the batch size of the operator. If no parameter has an additional dimension, the batch size isNone.- Returns:
Size of the parameter broadcasting dimension if present, else
None.- Return type:
int or None
- property c: Tensor¶
- static compute_decomposition(*params: TensorLike | Tensor | Number, wires: Wires | Iterable[Hashable] | Hashable | None = None, **hyperparameters: dict[str, Any])¶
Representation of the operator as a product of other operators (static method).
\[O = O_1 O_2 \dots O_n.\]Note
Operations making up the decomposition should be queued within the
compute_decompositionmethod.See also
decomposition().- Parameters:
*params (list) – trainable parameters of the operator, as stored in the
parametersattributewires (Iterable[Any], Wires) – wires that the operator acts on
**hyperparams (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparametersattribute
- Returns:
decomposition of the operator
- Return type:
list[Operator]
- static compute_matrix(*params, **hyperparams) Tensor¶
Representation of the operator as a canonical matrix in the computational basis (static method).
The canonical matrix is the textbook matrix representation that does not consider wires. Implicitly, this assumes that the wires of the operator correspond to the global wire order.
See also
Operator.matrix()andqml.matrix()- Parameters:
*params (list) – trainable parameters of the operator, as stored in the
parametersattribute**hyperparams (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparametersattribute
- Returns:
matrix representation
- Return type:
tensor_like
- property cs_wires: Wires¶
- property d: Tensor¶
- property device: device¶
- property dtype: dtype¶
- classmethod from_polar_params(r0: TensorLike | Tensor | Number | None = None, r1: TensorLike | Tensor | Number | None = None, theta0: TensorLike | Tensor | Number | None = None, theta1: TensorLike | Tensor | Number | None = None, theta2: TensorLike | Tensor | Number | None = None, theta3: TensorLike | Tensor | Number | None = None, theta4: TensorLike | Tensor | Number | None = None, *, wires=None, dtype: dtype = torch.complex128, device: device | None = None, **kwargs) MatchgateOperation¶
Matchgate from polar parameters.
They are the parameters of a Matchgate operation in the standard form which is a 4x4 matrix
\[ \begin{align}\begin{aligned}\begin{split} \begin{bmatrix} r_0 e^{i\theta_0} & 0 & 0 & (\sqrt{1 - r_0^2}) e^{-i(\theta_1+\pi)} \\ 0 & r_1 e^{i\theta_2} & (\sqrt{1 - r_1^2}) e^{-i(\theta_3+\pi)} & 0 \\ 0 & (\sqrt{1 - r_1^2}) e^{i\theta_3} & r_1 e^{-i\theta_2} & 0 \\ (\sqrt{1 - r_0^2}) e^{i\theta_1} & 0 & 0 & r_0 e^{-i\theta_0} \end{bmatrix}\end{split}\\where :math:`r_0, r_1, \theta_0, \theta_1, \theta_2, \theta_3, \theta_4` are the parameters.\end{aligned}\end{align} \]The polar parameters will be converted to standard parameterization. The conversion is given by
\[\begin{split}\begin{align} a &= r_0 e^{i\theta_0} \\ b &= (\sqrt{1 - r_0^2}) e^{i(\theta_2+\theta_4-(\theta_1+\pi))} \\ c &= (\sqrt{1 - r_0^2}) e^{i\theta_1} \\ d &= r_0 e^{i(\theta_2+\theta_4-\theta_0)} \\ w &= r_1 e^{i\theta_2} \\ x &= (\sqrt{1 - r_1^2}) e^{i(\theta_2+\theta_4-(\theta_3+\pi))} \\ y &= (\sqrt{1 - r_1^2}) e^{i\theta_3} \\ z &= r_1 e^{i\theta_4} \end{align}\end{split}\]
- classmethod from_std_params(a: TensorLike | Tensor | Number | None = None, b: TensorLike | Tensor | Number | None = None, c: TensorLike | Tensor | Number | None = None, d: TensorLike | Tensor | Number | None = None, w: TensorLike | Tensor | Number | None = None, x: TensorLike | Tensor | Number | None = None, y: TensorLike | Tensor | Number | None = None, z: TensorLike | Tensor | Number | None = None, *, wires=None, dtype: dtype = torch.complex128, device: device | None = None, **kwargs) MatchgateOperation¶
- classmethod from_sub_matrices(outer_matrix: TensorLike | Tensor | Number, inner_matrix: TensorLike | Tensor | Number, *, wires=None, dtype: dtype = torch.complex128, device: device | None = None, **kwargs)¶
- generator = None¶
- get_padded_single_particle_transition_matrix(wires=None) SingleParticleTransitionMatrixOperation¶
Return the padded single particle transition matrix in order to have the block diagonal form where the block is the single particle transition matrix at the corresponding wires.
- Parameters:
wires – The wires of the whole system.
- Returns:
padded single particle transition matrix
- grad_method = 'A'¶
- grad_recipe = None¶
Gradient recipe for the parameter-shift method.
This is a tuple with one nested list per operation parameter. For parameter \(\phi_k\), the nested list contains elements of the form \([c_i, a_i, s_i]\) where \(i\) is the index of the term, resulting in a gradient recipe of
\[\frac{\partial}{\partial\phi_k}f = \sum_{i} c_i f(a_i \phi_k + s_i).\]If
None, the default gradient recipe containing the two terms \([c_0, a_0, s_0]=[1/2, 1, \pi/2]\) and \([c_1, a_1, s_1]=[-1/2, 1, -\pi/2]\) is assumed for every parameter.- Type:
tuple(Union(list[list[float]], None)) or None
- property inner_gate_data¶
The gate data is the matrix
\[\begin{split}\begin{pmatrix} a & 0 & 0 & b \\ 0 & w & x & 0 \\ 0 & y & z & 0 \\ c & 0 & 0 & d \end{pmatrix}\end{split}\]where \(a, b, c, d, w, x, y, z \in \mathbb{C}\). The inner gate data is the following sub-matrix of the matchgate matrix:
\[\begin{split}\begin{pmatrix} w & x \\ y & z \end{pmatrix}\end{split}\]- Returns:
- label(decimals=None, base_label=None, cache=None)¶
A customizable string representation of the operator.
- Parameters:
decimals=None (int) – If
None, no parameters are included. Else, specifies how to round the parameters.base_label=None (str) – overwrite the non-parameter component of the label
cache=None (dict) – dictionary that carries information between label calls in the same drawing
- Returns:
label to use in drawings
- Return type:
str
Example:
>>> op = qml.RX(1.23456, wires=0) >>> op.label() "RX" >>> op.label(base_label="my_label") "my_label" >>> op = qml.RX(1.23456, wires=0, id="test_data") >>> op.label() "RX("test_data")" >>> op.label(decimals=2) "RX\n(1.23,"test_data")" >>> op.label(base_label="my_label") "my_label("test_data")" >>> op.label(decimals=2, base_label="my_label") "my_label\n(1.23,"test_data")"
If the operation has a matrix-valued parameter and a cache dictionary is provided, unique matrices will be cached in the
'matrices'key list. The label will contain the index of the matrix in the'matrices'list.>>> op2 = qml.QubitUnitary(np.eye(2), wires=0) >>> cache = {'matrices': []} >>> op2.label(cache=cache) 'U(M0)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True)] >>> op3 = qml.QubitUnitary(np.eye(4), wires=(0,1)) >>> op3.label(cache=cache) 'U(M1)' >>> cache['matrices'] [tensor([[1., 0.], [0., 1.]], requires_grad=True), tensor([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]], requires_grad=True)]
- ndim_params = 2¶
- num_params = 1¶
- num_wires: int | WiresEnum = 2¶
Number of wires the operator acts on.
- property outer_gate_data¶
The gate data is the matrix
\[\begin{split}\begin{pmatrix} a & 0 & 0 & b \\ 0 & w & x & 0 \\ 0 & y & z & 0 \\ c & 0 & 0 & d \end{pmatrix}\end{split}\]where \(a, b, c, d, w, x, y, z \in \mathbb{C}\). The outer gate data is the following sub-matrix of the matchgate matrix:
\[\begin{split}\begin{pmatrix} a & b \\ c & d \end{pmatrix}\end{split}\]- Returns:
The outer gate data.
- par_domain = 'A'¶
- classmethod random(wires: Wires, *, batch_size: int | None = None, dtype: dtype = torch.complex128, device: device | None = None, seed: int | None = None, **kwargs) MatchgateOperation¶
Generate a random MatchgateOperation instance.
This class method creates a new instance of MatchgateOperation with randomly initialized parameters. The generated object can be used for testing, experimentation, or random state initialization.
The random values for the parameters can be controlled using the provided seed. Additional configuration can be done using various optional parameters such as the batch size, data type, device, and any implementation-specific keyword arguments.
- Parameters:
wires (Wires) – Wires that the operation acts on. Can be a sequence or scalar.
batch_size – Optional size of generated random batches. If None, a single gate is returned.
dtype – Torch data type used for the random parameters. Defaults to torch.complex128.
device – Optional Torch device on which the parameters will be created. If None, parameters are created on the default device.
seed – Optional integer seed for controlling randomness. If None, the seed is not fixed.
kwargs – Additional keyword arguments for specific configuration or compatibility.
- Returns:
A new MatchgateOperation instance with randomly initialized parameters.
- Return type:
- classmethod random_params(*, batch_size: int | None = None, dtype: dtype = torch.float64, device: device | None = None, seed: int | None = None, **kwargs) TensorLike | Tensor | Number | MatchgateParams¶
Generates a tensor of random parameters. This method allows creating a tensor with random elements drawn from uniform (0, 1] for the two first elements and drawn from a uniform distribution [0, \(2\pi\)). If
batch_sizeis None, a single parameter set is returned instead of a batch.- Parameters:
batch_size (Optional[int]) – The number of parameter sets to generate. If None, only a single parameter set is generated.
dtype – The data type of the output tensor. Defaults to torch.float64.
device – The device where the tensor will be allocated. Defaults to the current default device if not specified.
seed (Optional[int]) – A manual seed for reproducible random generation. If None, the random generator is not seeded manually.
kwargs – Additional keyword arguments for extended configuration.
- Returns:
A tensor containing the generated random parameters. If
batch_sizeis specified, the shape will be(batch_size, 7). Otherwise, the returned tensor has shape(7,).- Return type:
torch.Tensor
- property shape: Tuple[int, int] | Tuple[int, int, int]¶
- property single_particle_transition_matrix¶
- property sorted_wires: Wires¶
- to_sptm_operation() SingleParticleTransitionMatrixOperation¶
- property w: Tensor¶
- property x: Tensor¶
- property y: Tensor¶
- property z: Tensor¶
matchcake.operations.rxx module¶
- class matchcake.operations.rxx.Rxx(theta: TensorLike, wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
Bases:
MatchgateOperationThis operation implements the following as a MatchgateOperation:
\[\begin{split}U = e^{-i\theta/2 X_{j} \otimes X_{j+1}} \\ = \begin{bmatrix} \cos(\theta/2) & 0 & 0 & -i\sin(\theta/2) \\ 0 & \cos(\theta/2) & -i\sin(\theta/2) & 0 \\ 0 & -i\sin(\theta/2) & \cos(\theta/2) & 0 \\ -i\sin(\theta/2) & 0 & 0 & \cos(\theta/2) \end{bmatrix}\end{split}\]where \(\theta\) is a parameter, \(X_{j}\) is the Pauli-X operator applied on the wire \(j\), and \(i\) is the imaginary unit.
- __init__(theta: TensorLike, wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
- num_params = 1¶
- num_wires: int | WiresEnum = 2¶
Number of wires the operator acts on.
matchcake.operations.rzz module¶
- class matchcake.operations.rzz.Rzz(theta: TensorLike, wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
Bases:
MatchgateOperationThis operation implements the following as a MatchgateOperation:
\[\begin{split}U = e^{-i\theta/2 Z_{j} \otimes Z_{j+1}} \\ = \begin{bmatrix} e^{-i\theta/2} & 0 & 0 & 0 \\ 0 & e^{i\theta/2} & 0 & 0 \\ 0 & 0 & e^{i\theta/2} & 0 \\ 0 & 0 & 0 & e^{-i\theta/2} \end{bmatrix}\end{split}\]where \(\theta\) is a parameter, \(Z_{j}\) is the Pauli-Z operator applied on the wire \(j\), and \(i\) is the imaginary unit.
- __init__(theta: TensorLike, wires=None, id=None, default_dtype: dtype = torch.complex128, default_device: device | None = None, **kwargs)¶
- num_params = 1¶
- num_wires: int | WiresEnum = 2¶
Number of wires the operator acts on.