Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Tket dependencies for PennyLane model #195

Open
wants to merge 40 commits into
base: main
Choose a base branch
from

Conversation

blakewilsonquantinuum
Copy link
Contributor

Currently, to make a pennylane circuit, we construct a tket circuit, extract the ops, and then construct the pennylane circuit from that. This requires tket being a middle man for any circuit conversion. Instead, we can convert to a preliminary diagram compatible with all circuits before converting to the particular backend.

make_circuital function - Built. Needs more testing.
Pulls qubits to the top of the diagram.
Pulls measurements to the bottom of the diagram.
Orders qubits and measurements so they are easy to build a circuit.
Type checks each layer to ensure a valid diagram.
Type checking has been tested on all diagrams from the mc_dataset.
is_circuital - Needs built.
Checks if a diagram is a valid circuital diagram.
Verifies qubits, measurements, and gates are all placed correctly and type checks.
started to build in the draft PR.
from_circuit - Needs built.
Function to build a tket, pennylane, or other circuit using the preliminary circuit.
Refactor tket, pennylane, and torch_quantum to use the above functions for building their circuits.

@dimkart dimkart changed the title Tketdepremoval Remove Tket dependencies for PennyLane model Dec 2, 2024
@blakewilsonquantinuum blakewilsonquantinuum marked this pull request as ready for review December 9, 2024 15:23
@blakewilsonquantinuum
Copy link
Contributor Author

2024-12-10_15-42-33.pdf

Comparable performance

image


Returns
-------
Dict:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a dedicated re-usable public class for the circuit dictionary, with specific attributes for each part/list. We might add more functionality in the future.

@dimkart
Copy link
Contributor

dimkart commented Dec 11, 2024

With this PR (or a follow-up), we should also move tket to optional dependencies in setup.cfg, e.g. like PennyLane. @nikhilkhatri @blakewilsonquantinuum is there any reason to keep it as a required dependency?

@dimkart
Copy link
Contributor

dimkart commented Dec 11, 2024

@blakewilsonquantinuum As discussed, the new implementation stay consistently behind the old ones in terms of times (although not much). It might be good to do some more detailed time profiling and detect any bottlenecks.

@@ -1912,14 +1970,14 @@ class Functor:
>>> n = Ty('n')
>>> diag = Cap(n, n.l) @ Id(n) >> Id(n) @ Cup(n.l, n)
>>> diag.draw(
... figsize=(2, 2), path='./snake.png')
... figsize=(2, 2), path='./docs/_static/images/snake.png')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be sure you pull the main again, this file here looks outdated (or some problem happened with conflict resolution).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, be sure to keep the upstream changes for these images above.

offset: int,
gates: list[Layer]) -> Tuple[list[Layer], list[Layer]]:

# Adds a qubit to the qubit list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using """ here?

elif not gate['type'] in OPTYPE_MAP:
raise NotImplementedError(f'Gate {gate} not supported')

if 'phase' in gate and gate['phase']:
Copy link
Contributor

@dimkart dimkart Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if gate.get('phase'): should do.

checks if a diagram is a quantum 'circuital' diagram.
A circuital diagram is a diagram with qubits at the top.

Used to check for measurements and post-selection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this referring to what the case was in the previous version? Is this information useful at all here?

@@ -362,6 +211,71 @@ def _tk_to_lmbq_param(theta):
raise ValueError('Parameter must be a (possibly scaled) sympy Symbol')


def to_tk(diagram: Diagram):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No return type here?



def to_pennylane(lambeq_circuit: Diagram, probabilities=False,
def to_pennylane(diagram: Diagram, probabilities=False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type.

return True


def to_circuital(diagram: Diagram):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type.

return layerD


def circuital_to_dict(diagram: Diagram):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type.

}


def tk_op_to_pennylane(tk_op):
def extract_ops_from_circuital(circuit_dict: dict):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type.

@dimkart
Copy link
Contributor

dimkart commented Dec 13, 2024

Many return types are missing, I have marked some but probably there are more, please check everywhere.

@dimkart
Copy link
Contributor

dimkart commented Dec 13, 2024

@blakewilsonquantinuum For the deepcopy speed issue. Can you try to do it with serialisation to see how it compares?

e.g. something like:

import pickle
serialized = pickle.dumps(my_object)
deep_copy = pickle.loads(serialized)

@blakewilsonquantinuum
Copy link
Contributor Author

The mypy issue above is unrelated to this PR. It comes from grammar.py when returning NotImplemented and is only flagged in the newest version of mypy (1.14.1 released on Dec 30 2024)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants