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

Use pauli_rep to compute qml.matrix() whenever possible #5392

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ff3e6ea
Updated condition for swapping matmul order
mudit2812 Mar 4, 2024
08b681e
Added tests
mudit2812 Mar 4, 2024
b1c4391
use pauli rep for computing matrix of Sum
Qottmann Mar 15, 2024
a90a15b
[ci skip]
Qottmann Mar 15, 2024
8a78e44
black formatting
Qottmann Mar 22, 2024
98202cf
black formatting
Qottmann Mar 22, 2024
a6a5de0
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Mar 26, 2024
bc04197
changelog
Qottmann Mar 26, 2024
0c359f5
Merge branch 'master' into summatrix
Qottmann Apr 3, 2024
d43fc1d
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 4, 2024
c5b869f
upgrade qml.matrix
Qottmann Apr 4, 2024
7e2b7f3
black formatting
Qottmann Apr 4, 2024
88f746e
remove changes to matrix
Qottmann Apr 4, 2024
93b3ac2
Merge branch 'pauli-swap-order' of https://github.com/PennyLaneAI/pen…
Qottmann Apr 4, 2024
54f642b
branch off mudits PR
Qottmann Apr 4, 2024
86bb86b
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 4, 2024
9580c13
merge
Qottmann Apr 4, 2024
7b317db
bugfix
Qottmann Apr 5, 2024
e77a606
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 5, 2024
c063fbb
[ci skip]
Qottmann Apr 5, 2024
d4ddba2
Merge branch 'master' into pauli-swap-order
mudit2812 Apr 9, 2024
58efe4c
[skip ci] Skip CI
mudit2812 Apr 9, 2024
56111e4
Merge branch 'master' into pauli-swap-order
mudit2812 Apr 9, 2024
4786179
[skip ci] Skip CI
mudit2812 Apr 9, 2024
e4cb275
Merge branch 'master' into pauli-swap-order
mudit2812 Apr 11, 2024
d209151
Merge branch 'master' into pauli-swap-order
mudit2812 Apr 17, 2024
743996e
Merge branch 'master' into pauli-swap-order
mudit2812 Apr 18, 2024
64af1ca
Fixed failing tests
mudit2812 Apr 18, 2024
6f16caa
Trigger CI
mudit2812 Apr 18, 2024
a618920
Updated qasm test
mudit2812 Apr 18, 2024
60bb55e
Merge branch 'master' into pauli-swap-order
mudit2812 Apr 18, 2024
50f8ae6
Merge branch 'pauli-swap-order' into summatrix
mudit2812 Apr 18, 2024
cbc3cad
remove commented code
Qottmann Apr 19, 2024
4bc3072
merge
Qottmann Apr 24, 2024
460243f
merge changelog
Qottmann Apr 24, 2024
2d76378
merge
Qottmann Apr 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@
[stim](https://github.com/quantumlib/Stim) `v1.13.0`.
[(#5409)](https://github.com/PennyLaneAI/pennylane/pull/5409)

* `qml.matrix` uses the pauli representation for matrix computation,
which results in faster computation of the matrix.
[(5392)](https://github.com/PennyLaneAI/pennylane/pull/5392)

* `default.mixed` has improved support for sampling-based measurements with non-numpy interfaces.
[(#5514)](https://github.com/PennyLaneAI/pennylane/pull/5514)

Expand Down
4 changes: 3 additions & 1 deletion pennylane/ops/functions/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ def circuit():
if isinstance(op, qml.operation.Tensor) and wire_order is not None:
op = 1.0 * op # convert to a Hamiltonian

if isinstance(op, qml.ops.Hamiltonian):
if (pr := op.pauli_rep) is not None:
return pr.to_mat(wire_order=wire_order or op.wires, format="csr").toarray()
Copy link
Member

Choose a reason for hiding this comment

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

Is CSR what we want to use for this?

Copy link
Contributor Author

@Qottmann Qottmann Apr 24, 2024

Choose a reason for hiding this comment

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

For context, this came up in switching to new opmath because some generators and qchem operators where now slower in computing qml.matrix(), as they were no longer ops.Hamiltonian instances and therefore didnt hit the op.sparse_matrix(..).toarray() method below anymore. The idea was to just allow still doing that via the pauli rep in this PR; but as @albi3ro pointed out this breaks differentiability. Afaik christina has been cooking up an alternate solution 👩‍🍳


if isinstance(op, qml.ops.Hamiltonian):
return op.sparse_matrix(wire_order=wire_order).toarray()

try:
Expand Down
4 changes: 2 additions & 2 deletions tests/ops/functions/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ def circuit():
qml.matrix(circuit, wire_order=wires)()

with pytest.raises(
TransformError,
match=r"Wires in circuit \[0\] are inconsistent with those in wire_order \[1\]",
ValueError,
match=r"the matrix for the specified wire order because it does not contain all the Pauli sentence",
):
qml.matrix(qml.PauliX(0), wire_order=[1])

Expand Down
Loading