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

Pattern matching for quantum circuit optimization #2032

Merged
merged 174 commits into from
Mar 25, 2022
Merged

Conversation

rmoyard
Copy link
Contributor

@rmoyard rmoyard commented Dec 15, 2021

Context:

https://arxiv.org/pdf/1909.05270.pdf

Description of the Change:

Introduce a new transform that takes a list of patterns as parameter and returns an optimized version of the circuit.

Example

def circuit():
    qml.Toffoli(wires=[3, 4, 0])
    qml.CNOT(wires=[1, 4])
    qml.CNOT(wires=[2, 1])
    qml.Hadamard(wires=3)
    qml.PauliZ(wires=1)
    qml.CNOT(wires=[2, 3])
    qml.Toffoli(wires=[2, 3, 0])
    qml.CNOT(wires=[1, 4])
    return qml.expval(qml.PauliX(wires=0))

with qml.tape.QuantumTape() as template:
    qml.CNOT(wires=[1, 2])
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[1, 2])
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[0, 2])

dev = qml.device('default.qubit', wires=8)
qnode = qml.QNode(circuit_example, dev)
print(qml.draw(qnode)())

optimized_qfunc = qml.transforms.pattern_matching(pattern_tapes=[template])(circuit_example)
optimized_qnode = qml.QNode(optimized_qfunc, dev)
print(qml.draw(optimized_qnode)())

Figure_1

Figure_2

self.max_match_list.append(Match(sorted(matches.match), matches.qubit))


class SubstitutionConfig: # pylint: disable=too-many-arguments, too-few-public-methods
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be a good candidate for a dataclass.

Copy link
Member

Choose a reason for hiding this comment

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

💯

self.circuit_blocked[succ] = True


class Match: # pylint: disable=too-few-public-methods
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be done with a named tuple instead?


.. code-block:: python

def circuit():
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it would be better to start with a blatantly obvious example, where a human can see the pattern without having to stare at it for too long, and then show the more complicated template example.

Like the template is something like [qml.S(0), qml.S(0), qml.PauliZ(0)].


.. code-block:: python

def circuit():
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it would be better to start with a blatantly obvious example, where a human can see the pattern without having to stare at it for too long, and then show the more complicated template example.

Like the template is something like [qml.S(0), qml.S(0), qml.PauliZ(0)].

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Also changelog.

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Just a few comments on the changelog.

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
2: ──Z──────╰Z─┤
```

Note that with this pattern we also replace a ``pennylane.S``, ``pennylane.PauliZ`` sequence by ``pennylane.S``.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think at this point we can just point the reader to the documentation and paper. The point of examples in the changelog is to catch interest, not explain in detail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok great I will remove this part!

rmoyard and others added 4 commits March 25, 2022 16:55
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Looks great! Lot of work here and quite an achievement 🥇

@rmoyard rmoyard merged commit 7dee6fe into master Mar 25, 2022
@rmoyard rmoyard deleted the pattern_matching branch March 25, 2022 20:33
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.

None yet

6 participants