Ansatz Factory

AnsatzFactory

A base class for ansatz factories.

AnsatzFactory

class mrmustard.physics.ansatz_factory.AnsatzFactory(ansatz_dict, **kwargs)[source]

A base class for ansatz factories. Takes a function that represents the ansätze and generates new ansätze given parameters. Caches the ansätze for given parameters and representation.

Parameters:
  • ansatz_dict (Mapping[ReprEnum, tuple[Callable[..., Ansatz], tuple[str, ...]]]) – A dictionary mapping ReprEnum to a tuple containing a function that generates the respective ansätze and a list of its parameter names.

  • **kwargs – Additional keyword arguments to pass to the ansatz functions.

Raises:

ValueError – If the AnsatzFactory is initialized with an empty dictionary.

property ansatz_dict: Mapping[ReprEnum, tuple[Callable[[...], Ansatz], tuple[str, ...]]]

The ansatz generating functions of this ansatz factory.

property additional_args: dict[str, Any]

The additional arguments to pass into the ansatz functions.

classmethod from_ansatz(ansatz, representation=None)[source]

Creates an AnsatzFactory from an Ansatz.

Parameters:
  • ansatz (Ansatz) – The ansatz to create the AnsatzFactory from.

  • representation (ReprEnum | None) – The representation of the ansatz. If None, it will be inferred from the ansatz.

Returns:

A tuple containing an AnsatzFactory with the given ansatz and the representation of the ansatz.

Return type:

tuple[AnsatzFactory, ReprEnum]

get_cached_ansatz(representation, **kwargs)[source]

Retrieves the cached ansatz for the given kwargs. Returns None if caching should be skipped.

Parameters:
  • representation (ReprEnum) – The representation of the ansatz.

  • **kwargs (Any) – The arguments used to generate the ansatz.

Returns:

The cached ansatz for the given kwargs.

Return type:

Ansatz | None