CQCL / lambeq

A high-level Python library for Quantum Natural Language Processing
https://docs.quantinuum.com/lambeq/
Apache License 2.0
455 stars 111 forks source link

Implemented Anastz Customization #130 #134

Closed valleyofblackpanther closed 6 months ago

valleyofblackpanther commented 8 months ago

Hi @neiljdo Added a new custom method to the BaseAnsatz class called custom, which implements BaseAnsatz.custom function, where anything can go inside that custom function, even custom gate definitions. I still think there needs to be modifications in this implementation, so far I modified the following:

  1. class BasenAnsatz: To add a custom function for creating and using custom quantum gates within the BaseAnsatz class, which can then be utilized by any subclass such as CircuitAnsatz, you would define a method in BaseAnsatz for handling the custom gate logic. This method would ideally accept parameters for the gate's unitary matrix and the qubits it acts upon. Subclasses can then leverage this method to incorporate custom gates into their circuits.

Note: The code aims to provide a flexible framework for defining and applying custom quantum gates within quantum circuits. By allowing the user to specify a unitary matrix, any conceivable quantum operation can be implemented, provided it adheres to the unitary constraint.

  1. class CircuitAnsatz: When defining specific ansatzes in subclasses, we can now utilize the add_custom_gate method to include custom gates as part of the circuit construction.
  2. class CustomGate: To complete the integration of custom gates within the Diagram class, we need to implement the logic for inserting a CustomGate into the diagram's structure. This involves creating a new layer or incorporating the custom gate into an existing layer.
  3. class Diagram / Diagram Structure: The implementation appends the custom gate as a new layer at the end of the diagram. Depending on the needs, we might want to insert the custom gate at a specific position or merge it with existing layers.
  4. def create_layer_for_custom_gate:

Dynamic Layer Composition: This method dynamically constructs a new layer, considering the qubits the custom gate acts upon. It initializes the layer with identity operations (Id(qubit)) for all qubits and then replaces the relevant positions with the custom gate.

Handling of left and right Types: The left type is determined based on the diagram's existing structure, while the right type is adjusted to accommodate the maximum qubit index involved with the custom gate. This ensures the layer fits seamlessly into the diagram.

Layer Insertion: The new layer is appended to the end of the diagram. For more complex scenarios, such as inserting the layer at a specific position or integrating it with existing layers, additional logic would be required.

I still think there needs to be some kind of modifications to this implementation, this could also be seen as a proof of a method. But any kind of review would be a great help!

dimkart commented 6 months ago

@valleyofblackpanther This will be closed for now, please follow-up discussion in Issue #130. It can be re-opened later if required.