Skip to content

Commit

Permalink
Merge branch 'master' into dm-wires
Browse files Browse the repository at this point in the history
  • Loading branch information
eddddddy authored May 25, 2023
2 parents 7d16bae + 8af8514 commit 521fa52
Show file tree
Hide file tree
Showing 58 changed files with 1,706 additions and 434 deletions.
6 changes: 6 additions & 0 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Pending deprecations

These keys are still accessible in v0.31 and will be removed in v0.32.

* ``QuantumScript``'s ``name`` keyword argument and property are deprecated.
This also affects ``QuantumTape`` and ``OperationRecorder``.

- Deprecated in v0.31
- Will be removed in v0.32

* The ``observables`` argument in ``QubitDevice.statistics`` is deprecated. Please use ``circuit``
instead. Using a list of observables in ``QubitDevice.statistics`` is deprecated. Please use a
``QuantumTape`` instead.
Expand Down
31 changes: 30 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
given subspace.
[(#2845)](https://github.com/PennyLaneAI/pennylane/pull/2845)

* Added the `TRY` qutrit rotation operator, which allows applying a Y rotation on a
given subspace.
[(#2846)](https://github.com/PennyLaneAI/pennylane/pull/2846)

<h3>Improvements 🛠</h3>

* `DiagonalQubitUnitary` now decomposes into `RZ`, `IsingZZ` and `MultiRZ` gates
Expand Down Expand Up @@ -70,6 +74,10 @@
the hash of any operators/measurement processes.
[(#4087)](https://github.com/PennyLaneAI/pennylane/pull/4087)

* All drawing methods changed their default value for the keyword argument `show_matrices`
to `True`. This allows quick insights into broadcasted tapes for example.
[(#3920)](https://github.com/PennyLaneAI/pennylane/pull/3920)

* Support for adjoint differentiation has been added to the `DefaultQubit2` device.
[(#4037)](https://github.com/PennyLaneAI/pennylane/pull/4037)

Expand Down Expand Up @@ -113,11 +121,23 @@
* The new device interface in integrated with `qml.execute` for autograd, backpropagation, and no differentiation.
[(#3903)](https://github.com/PennyLaneAI/pennylane/pull/3903)

* `CZ` now inherits from the `ControlledOp` class. It now supports exponentiation to arbitrary powers with `pow`, which is no longer limited to integers. It also supports `sparse_matrix` and `decomposition` representations.
[(#4117)](https://github.com/PennyLaneAI/pennylane/pull/4117)

* The construction of the pauli representation for the `Sum` class is now faster.
[(#4142)](https://github.com/PennyLaneAI/pennylane/pull/4142)

* Updated the `gradients` module to use the new `Shots` object internally.
[(#4152)](https://github.com/PennyLaneAI/pennylane/pull/4152)

* The new device interface in integrated with `qml.execute` for Jax.
[(#4137)](https://github.com/PennyLaneAI/pennylane/pull/4137)

<h3>Breaking changes 💔</h3>

* All drawing methods changed their default value for the keyword argument `show_matrices` to `True`.
[(#3920)](https://github.com/PennyLaneAI/pennylane/pull/3920)

* `DiagonalQubitUnitary` does not decompose into `QubitUnitary` any longer, but into `RZ`, `IsingZZ`
and `MultiRZ` gates.
[(#4035)](https://github.com/PennyLaneAI/pennylane/pull/4035)
Expand All @@ -141,6 +161,10 @@

* `Operation.base_name` is deprecated. Please use `Operation.name` or `type(op).__name__` instead.

* ``QuantumScript``'s ``name`` keyword argument and property are deprecated.
This also affects ``QuantumTape`` and ``OperationRecorder``.
[(#4141)](https://github.com/PennyLaneAI/pennylane/pull/4141)

* `qml.grouping` module is removed. The functionality has been reorganized in the `qml.pauli` module.

<h3>Documentation 📝</h3>
Expand All @@ -158,18 +182,23 @@
* Removes a patch in `interfaces/autograd.py` that checks for the `strawberryfields.gbs` device. That device
is pinned to PennyLane <= v0.29.0, so that patch is no longer necessary.

* `qml.pauli.are_identical_pauli_words` now treats all identities as equal. Identity terms on Hamiltonians with non-standard
wire orders are no longer eliminated.
[(#4161)](https://github.com/PennyLaneAI/pennylane/pull/4161)

<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):

Isaac De Vlugt,
Soran Jahangiri,
Edward Jiang,
Korbinian Kottmann
Korbinian Kottmann,
Christina Lee,
Vincent Michaud-Rioux,
Romain Moyard,
Mudit Pandey,
Borja Requena,
Matthew Silverman,
Jay Soni,
David Wierichs.
2 changes: 1 addition & 1 deletion pennylane/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _process_shot_sequence(shot_list):
else:
# Iterate through the shots, and group consecutive identical shots
split_at_repeated = np.split(shot_list, np.diff(shot_list).nonzero()[0] + 1)
shot_vector = [ShotTuple(shots=i[0], copies=len(i)) for i in split_at_repeated]
shot_vector = [ShotTuple(shots=int(i[0]), copies=len(i)) for i in split_at_repeated]

elif all(isinstance(s, (int, tuple)) for s in shot_list):
# shot list contains tuples; assume it is already in a sparse representation
Expand Down
1 change: 1 addition & 0 deletions pennylane/devices/default_qutrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class DefaultQutrit(QutritDevice):
"TSWAP",
"THadamard",
"TRX",
"TRY",
}

# Identity is supported as an observable for qml.state() to work correctly. However, any
Expand Down
19 changes: 12 additions & 7 deletions pennylane/drawer/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def draw(
show_all_wires=False,
decimals=2,
max_length=100,
show_matrices=False,
show_matrices=True,
expansion_strategy=None,
):
"""Create a function that draws the given qnode or quantum function.
Expand Down Expand Up @@ -100,15 +100,15 @@ def circuit(a, w):
>>> print(qml.draw(circuit2)("x"))
0: ──RX(x)─┤ <Z>
When requested with ``show_matrices=True``, matrix valued parameters are printed below the
circuit:
When requested with ``show_matrices=True`` (the default), matrix valued parameters
are printed below the circuit. For ``show_matrices=False``, they are not printed:
>>> @qml.qnode(qml.device('default.qubit', wires=2))
... def circuit3():
... qml.QubitUnitary(np.eye(2), wires=0)
... qml.QubitUnitary(-np.eye(4), wires=(0,1))
... return qml.expval(qml.Hermitian(np.eye(2), wires=1))
>>> print(qml.draw(circuit3, show_matrices=True)())
>>> print(qml.draw(circuit3)())
0: ──U(M0)─╭U(M1)─┤
1: ────────╰U(M1)─┤ <𝓗(M0)>
M0 =
Expand All @@ -119,6 +119,9 @@ def circuit(a, w):
[-0. -1. -0. -0.]
[-0. -0. -1. -0.]
[-0. -0. -0. -1.]]
>>> print(qml.draw(circuit3, show_matrices=False)())
0: ──U(M0)─╭U(M1)─┤
1: ────────╰U(M1)─┤ <𝓗(M0)>
The ``max_length`` keyword warps long circuits:
Expand Down Expand Up @@ -232,7 +235,7 @@ def _draw_qnode(
show_all_wires=False,
decimals=2,
max_length=100,
show_matrices=False,
show_matrices=True,
expansion_strategy=None,
):
@wraps(qnode)
Expand Down Expand Up @@ -261,10 +264,12 @@ def wrapper(*args, **kwargs):
)
for t in tapes[0]
]
if show_matrices:
mat_str = "\n"
if show_matrices and cache["matrices"]:
mat_str = ""
for i, mat in enumerate(cache["matrices"]):
mat_str += f"\nM{i} = \n{mat}"
if mat_str:
mat_str = "\n" + mat_str
return "\n\n".join(res) + mat_str
return "\n\n".join(res)

Expand Down
11 changes: 6 additions & 5 deletions pennylane/drawer/tape_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def tape_text(
show_all_wires=False,
decimals=None,
max_length=100,
show_matrices=False,
show_matrices=True,
cache=None,
):
"""Text based diagram for a Quantum Tape.
Expand All @@ -116,7 +116,7 @@ def tape_text(
Default ``None`` will omit parameters from operation labels.
max_length (Int) : Maximum length of a individual line. After this length, the diagram will
begin anew beneath the previous lines.
show_matrices=False (bool): show matrix valued parameters below all circuit diagrams
show_matrices=True (bool): show matrix valued parameters below all circuit diagrams
cache (dict): Used to store information between recursive calls. Necessary keys are ``'tape_offset'``
and ``'matrices'``.
Expand Down Expand Up @@ -204,7 +204,7 @@ def tape_text(
Matrix valued parameters are always denoted by ``M`` followed by an integer corresponding to
unique matrices. The list of unique matrices can be printed at the end of the diagram by
selecting ``show_matrices=True``:
selecting ``show_matrices=True`` (the default):
.. code-block:: python
Expand All @@ -213,7 +213,7 @@ def tape_text(
qml.QubitUnitary(np.eye(2), wires=1)
qml.expval(qml.Hermitian(np.eye(4), wires=(0,1)))
>>> print(qml.drawer.tape_text(tape, show_matrices=True))
>>> print(qml.drawer.tape_text(tape))
0: ──U(M0)─┤ ╭<𝓗(M1)>
1: ──U(M0)─┤ ╰<𝓗(M1)>
M0 =
Expand All @@ -230,7 +230,7 @@ def tape_text(
tape offset.
>>> cache = {'matrices': [-np.eye(3)]}
>>> print(qml.drawer.tape_text(tape, show_matrices=True, cache=cache))
>>> print(qml.drawer.tape_text(tape, cache=cache))
0: ──U(M1)─┤ ╭<𝓗(M2)>
1: ──U(M1)─┤ ╰<𝓗(M2)>
M0 =
Expand Down Expand Up @@ -332,6 +332,7 @@ def tape_text(
totals = [filler.join([t, s]) for t, s in zip(totals, layer_str)]
if ender:
totals = [s + "─┤" for s in totals]
line_length += 2

# Recursively handle nested tapes #
tape_totals = "\n".join(finished_lines + totals)
Expand Down
28 changes: 11 additions & 17 deletions pennylane/gradients/finite_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
from scipy.special import factorial

import pennylane as qml
from pennylane._device import _get_num_copies
from pennylane.measurements import ProbabilityMP
from pennylane.measurements import ProbabilityMP, Shots

from .general_shift_rules import generate_shifted_tapes
from .gradient_transform import (
Expand Down Expand Up @@ -157,21 +156,15 @@ def finite_diff_coeffs(n, approx_order, strategy):
return coeffs_and_shifts


def _processing_fn(results, shots=None, single_shot_batch_fn=None):
shot_vector = isinstance(shots, Sequence)

if not shot_vector:
grads_tuple = single_shot_batch_fn(results)
else:
grads_tuple = []
len_shot_vec = _get_num_copies(shots)
for idx in range(len_shot_vec):
res = [tape_res[idx] for tape_res in results]
g_tuple = single_shot_batch_fn(res)
grads_tuple.append(g_tuple)
grads_tuple = tuple(grads_tuple)

return grads_tuple
def _processing_fn(results, shots: Shots = Shots(None), single_shot_batch_fn=None):
if not shots.has_partitioned_shots:
return single_shot_batch_fn(results)
grads_tuple = []
for idx in range(shots.num_copies):
res = [tape_res[idx] for tape_res in results]
g_tuple = single_shot_batch_fn(res)
grads_tuple.append(g_tuple)
return tuple(grads_tuple)


@gradient_transform
Expand Down Expand Up @@ -345,6 +338,7 @@ def finite_diff(
validate_params=validate_params,
shots=shots,
)
shots = Shots(shots)
if argnum is None and not tape.trainable_params:
return _no_trainable_grad(tape, shots)

Expand Down
Loading

0 comments on commit 521fa52

Please sign in to comment.