forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
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
[WIP] Refactor various parts of qiskit using multiple dispatch #1
Open
jlapeyre
wants to merge
19
commits into
master
Choose a base branch
from
multiple-dispatch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
modified: qiskit/opflow/gradients/gradient.py
502138a
to
ae5aed5
Compare
2f266b5
to
132f32e
Compare
132f32e
to
faf76e6
Compare
fd93d40
to
ce78a7a
Compare
jlapeyre
pushed a commit
that referenced
this pull request
Jun 5, 2022
* rm deprecated algo methods * add reno * fix tests, remove from varalgo * intial point was said to be abstract in varalgo! * attempt to fix sphinx #1 of ? Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
jlapeyre
pushed a commit
that referenced
this pull request
Jul 19, 2023
* Feat: Add `get_causal_node` to `DAGCircuit`: - Also added `get_qubit_input_node` and `get_qubit_output_node`. * Test: Added tests to `dagcircuit.py` * Docs: Added release note * Chore: Remove type-checking in `dagcircuit.py` - Type checking was causing strange behavior during linting. * Added changes to speed up get_causal_cone (#1) - Replace lists with deque for the iteration. * Docs: Modify docstring and release note * Fix: Wrong comparison in `_get_input_output_node` * Remove: input and output node methods. * Lint: Fixed formatting * Docs: Fixed release-note * Docs: Fixed docstring and release note. * Fix: Output map double-lookup. * Docs: Fix inline comments. * Test: Added test for circuits with barriers * Refactor: rename to `quantum_causal_cone` * FIx: Use quantum_sucessors and docstring --------- Co-authored-by: danielleodigie <97267313+danielleodigie@users.noreply.github.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR refactors some code using multiple dispatch. This is an experiment in applying multiple dispatch in
various situations. The experiments are mostly spread sparsely across the repo. This comment is a WIP, as well.
This PR requires the master branch of plum-dispatch. You can get this by cloning
plum-dispatch and doing, for example in a virtual env,
pip install -e .
.Advantages/Motivation
isinstance
conditionals, making the logic more clear.Especially useful with mathematical operators. This is by far the heaviest use case in Julia. This is difficult in python because operators must be class methods that privilege one operand. A workaround is to use function calls rather than infix operators.
__mul__
and__rmul__
for several classes all call a function (not method)multiple
which supports multiple dispatch.types representing lists and tuples of homogeneous type simplify logic. An example (not in this PR) is
https://github.com/jlapeyre/qiskit-terra/blob/253339278eb4e706805ae57653524f67dc8ad6f3/qiskit/opflow/gradients/circuit_gradients/lin_comb.py#L142-L144
is replaced by something like
Multiple-dispatch libraries
Multiple dispatch or multimethods in languages such as Dylan and Common Lisp (as CLOS) is older than the Julia language.
However, much of the motivation and design choices of current Python implementations come from Julia.
*
, are supported. They do not enter into dispatch. This is similar to JuliaNotFoundLookupError
is raised. One could add a feature to plum to print closest matching methods, as Julia does.MethodError
.Design issues
Other considerations
Storing type-like information in various places
In qiskit, we sometimes distinguish "kinds" of objects by attributes; a flag or a string. Or by checking whether a method exists. This design complicates MD and makes it more difficult to apply broadly. I would be a good idea to move these things into the type system. The easiest way is to make another type rather than a use a flag. But, maybe there is also a way to use dispatch with a trait system.
Does a multiple-dispatch system turn Python into Julia? No. The blurred distinction between run time and compile time in Julia offers an advantage that is difficult to reproduce in Python (compilation itself is a big problem in Python). First, let's be more precise by what we mean by multiple dispatch.
Multiple dispatch vs. operator overloading
MD is distinguished from function overloading in that the former is dynamic while the latter is static. Dispatch in the former is done on the dynamic type, in the latter it is done on the statically, lexically, analyzed type. Consider the code
where we have three abstract types with the relation
Animal <: Thing
,Machine <: Thing
, and another typeDog <: Animal
. We might have methodsWhat happens when I call
func(dog)
? In an operator-overloading language I get "cry", the static, lexically, deterimined type ofa
. In a MD language, dispatch is on the actual type of the object; I get "bark".Run-time compilation and inference
But, an important advantage is present in both static languages and Julia that is not available in Python and CLOS. In Julia, when
func(dog)
is first called, the types of all expressions in the body are inferred if possible and code is compiled specially for the inferred types. In particular, although this is "dynamic dispatch", the methodmake_noise
is devirtualized, and inlined if advantageous. This strategy may be pursued to any depth (there are algorithms to determine when it is advantageous not to specialize). One thereby obtains advantages of both static and dynamic languages.No diagonal dispatch. Some (all?) MD libraries' docs claim they do no support diagonal dispatch. I think the issue more broadly is that they do not support proper parametric types, in particular type parameters in the parameter lists. plum claims it has a method to get around lack of diagonal dispatch (using
Self
). But, I don't the problem worked around bySelf
is stated correctly. It is that@dispatch
can't recognize the type of a class inside the definition of the class, because is net yet recognized by python.Self
is in some sense a way to defer referring to the class by name.No universal, coherent type system.