Skip to content

Commit 5f7ec9b

Browse files
mudit2812actions-userMandrenkovdwierichsQottmann
authored
Merge dev into master and delete dev (#1257)
As name says. Once this PR is merged, `dev` will be deleted either automatically on merge, or manually if not. --------- Co-authored-by: GitHub Nightly Merge Action <actions@github.com> Co-authored-by: Mikhail Andrenkov <mikhail@xanadu.ai> Co-authored-by: David Wierichs <davidwierichs@gmail.com> Co-authored-by: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com> Co-authored-by: Ivana Kurečić <ivana@xanadu.ai> Co-authored-by: Paul Finlay <50180049+doctorperceptron@users.noreply.github.com> Co-authored-by: Jack Brown <jack.brown486@gmail.com> Co-authored-by: bellekaplan <167111433+bellekaplan@users.noreply.github.com> Co-authored-by: Utkarsh <utkarshazad98@gmail.com> Co-authored-by: Pietropaolo Frisoni <pietropaolo.frisoni@xanadu.ai> Co-authored-by: Guillermo Alonso-Linaje <65235481+KetpuntoG@users.noreply.github.com> Co-authored-by: gmlej <gmlejarza@gmail.com> Co-authored-by: Jorge J. Martínez de Lejarza <61199780+gmlejarza@users.noreply.github.com> Co-authored-by: ixfoduap <40441298+ixfoduap@users.noreply.github.com>
1 parent febe5f9 commit 5f7ec9b

27 files changed

+607
-167
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ environment:
7676
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane-cirq.git#egg=pennylane-cirq;\
7777
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane-qiskit.git#egg=pennylane-qiskit;\
7878
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane-qulacs.git#egg=pennylane-qulacs;\
79-
$$PYTHON_VENV_PATH/bin/python -m pip install --extra-index-url https://test.pypi.org/simple/ PennyLane-Lightning --pre --upgrade;\
8079
$$PYTHON_VENV_PATH/bin/python -m pip install --extra-index-url https://test.pypi.org/simple/ PennyLane-Catalyst --pre --upgrade;\
80+
$$PYTHON_VENV_PATH/bin/python -m pip install --extra-index-url https://test.pypi.org/simple/ PennyLane-Lightning --pre --upgrade;\
8181
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane.git#egg=pennylane;\
8282
fi;\
8383
fi
Loading

conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@
110110

111111
# Raise PennyLane deprecation warnings as errors
112112
warnings.filterwarnings("error", category=PennyLaneDeprecationWarning)
113+
warnings.filterwarnings(
114+
"ignore", message="Device will no longer be accessible", category=PennyLaneDeprecationWarning
115+
)
113116

114117
# Add any paths that contain templates here, relative to this directory.
115118
templates_path = ["_templates"]

demonstrations/tutorial_backprop.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
Let's have a go implementing the parameter-shift rule manually in PennyLane.
5656
"""
5757
import pennylane as qml
58-
from jax import numpy as np
58+
from jax import numpy as jnp
5959
from matplotlib import pyplot as plt
6060
import jax
6161

@@ -69,19 +69,28 @@
6969
# create a device to execute the circuit on
7070
dev = qml.device("default.qubit", wires=3)
7171

72+
73+
def CNOT_ring(wires):
74+
"""Apply CNOTs in a ring pattern"""
75+
n_wires = len(wires)
76+
77+
for w in wires:
78+
qml.CNOT([w % n_wires, (w + 1) % n_wires])
79+
80+
7281
@qml.qnode(dev, diff_method="parameter-shift")
7382
def circuit(params):
7483
qml.RX(params[0], wires=0)
7584
qml.RY(params[1], wires=1)
7685
qml.RZ(params[2], wires=2)
7786

78-
qml.broadcast(qml.CNOT, wires=[0, 1, 2], pattern="ring")
87+
CNOT_ring(wires=[0, 1, 2])
7988

8089
qml.RX(params[3], wires=0)
8190
qml.RY(params[4], wires=1)
8291
qml.RZ(params[5], wires=2)
8392

84-
qml.broadcast(qml.CNOT, wires=[0, 1, 2], pattern="ring")
93+
CNOT_ring(wires=[0, 1, 2])
8594
return qml.expval(qml.PauliY(0) @ qml.PauliZ(2))
8695

8796

@@ -109,10 +118,10 @@ def circuit(params):
109118

110119
def parameter_shift_term(qnode, params, i):
111120
shifted = params.copy()
112-
shifted = shifted.at[i].add(np.pi/2)
121+
shifted = shifted.at[i].add(jnp.pi/2)
113122
forward = qnode(shifted) # forward evaluation
114123

115-
shifted = shifted.at[i].add(-np.pi)
124+
shifted = shifted.at[i].add(-jnp.pi)
116125
backward = qnode(shifted) # backward evaluation
117126

118127
return 0.5 * (forward - backward)
@@ -125,7 +134,7 @@ def parameter_shift_term(qnode, params, i):
125134
# to loop over the index ``i``:
126135

127136
def parameter_shift(qnode, params):
128-
gradients = np.zeros([len(params)])
137+
gradients = jnp.zeros([len(params)])
129138

130139
for i in range(len(params)):
131140
gradients = gradients.at[i].set(parameter_shift_term(qnode, params, i))
@@ -147,7 +156,7 @@ def parameter_shift(qnode, params):
147156
# Alternatively, we can directly compute quantum gradients of QNodes using
148157
# PennyLane's built in :mod:`qml.gradients <pennylane.gradients>` module:
149158

150-
print(np.stack(qml.gradients.param_shift(circuit)(params)))
159+
print(jnp.stack(qml.gradients.param_shift(circuit)(params)))
151160

152161
##############################################################################
153162
# If you count the number of quantum evaluations, you will notice that we had to evaluate the circuit
@@ -372,10 +381,10 @@ def circuit(params):
372381
t = timeit.repeat("grad_qnode_backprop(params)", globals=globals(), number=num, repeat=reps)
373382
gradient_backprop.append([num_params, min(t) / num])
374383

375-
gradient_shift = np.array(gradient_shift).T
376-
gradient_backprop = np.array(gradient_backprop).T
377-
forward_shift = np.array(forward_shift).T
378-
forward_backprop = np.array(forward_backprop).T
384+
gradient_shift = jnp.array(gradient_shift).T
385+
gradient_backprop = jnp.array(gradient_backprop).T
386+
forward_shift = jnp.array(forward_shift).T
387+
forward_backprop = jnp.array(forward_backprop).T
379388

380389
##############################################################################
381390
# We now import matplotlib, and plot the results.
@@ -419,8 +428,8 @@ def circuit(params):
419428
# perform a least squares regression to determine the linear best fit/gradient
420429
# for the normalized time vs. number of parameters
421430
x = gradient_shift[0]
422-
m_shift, c_shift = np.polyfit(*gradient_shift, deg=1)
423-
m_back, c_back = np.polyfit(*gradient_backprop, deg=1)
431+
m_shift, c_shift = jnp.polyfit(*gradient_shift, deg=1)
432+
m_back, c_back = jnp.polyfit(*gradient_backprop, deg=1)
424433

425434
ax.plot(x, m_shift * x + c_shift, '--', label=f"{m_shift:.2f}p{c_shift:+.2f}")
426435
ax.plot(x, m_back * x + c_back, '--', label=f"{m_back:.2f}p{c_back:+.2f}")

demonstrations/tutorial_bluequbit.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
import matplotlib.pyplot as plt
4242
import numpy as np
4343

44+
# This filter will suppress deprecation warnings for viewability
45+
import warnings
46+
warnings.filterwarnings("ignore", "QubitDevice", qml.PennyLaneDeprecationWarning)
47+
4448

4549
def bell_pair():
4650
qml.Hadamard(0)

demonstrations/tutorial_diffable-mitigation.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
Thus, we can improve the estimates of observables without breaking the differentiable workflow of our variational algorithm.
4343
We will briefly introduce these functionalities and afterwards go more in depth to explore what happens under the hood.
4444
45-
We start by initializing a noisy device under the :class:`~.pennylane.DepolarizingChannel`:
45+
We start by initializing a noisy device using a noise model with :class:`~.pennylane.DepolarizingChannel` errors:
4646
"""
4747

4848
import pennylane as qml
@@ -54,13 +54,14 @@
5454
n_wires = 4
5555
np.random.seed(1234)
5656

57-
# Describe noise
58-
noise_gate = qml.DepolarizingChannel
59-
noise_strength = 0.05
57+
# Describe noise model
58+
fcond = qml.noise.wires_in(range(n_wires))
59+
noise = qml.noise.partial_wires(qml.DepolarizingChannel, 0.05)
60+
noise_model = qml.NoiseModel({fcond: noise})
6061

6162
# Load devices
6263
dev_ideal = qml.device("default.mixed", wires=n_wires)
63-
dev_noisy = qml.transforms.insert(dev_ideal, noise_gate, noise_strength, position="all")
64+
dev_noisy = qml.add_noise(dev_ideal, noise_model=noise_model)
6465

6566
##############################################################################
6667
# We are going to use the transverse field Ising model Hamiltonian :math:`H = - \sum_i X_i X_{i+1} + 0.5 \sum_i Z_i` as our observable:
@@ -85,8 +86,9 @@ def qfunc(w1, w2):
8586
qml.SimplifiedTwoDesign(w1, w2, wires=range(n_wires))
8687
return qml.expval(H)
8788

88-
qnode_noisy = qml.QNode(qfunc, dev_noisy)
8989
qnode_ideal = qml.QNode(qfunc, dev_ideal)
90+
qnode_noisy = qml.QNode(qfunc, dev_noisy)
91+
qnode_noisy = qml.transforms.decompose(qnode_noisy, gate_set = ["RY", "CZ"])
9092

9193
##############################################################################
9294
# We can then simply transform the noisy QNode :math:`f^{⚡}` with :func:`~.pennylane.transforms.mitigate_with_zne` to generate :math:`\tilde{f}.`

demonstrations/tutorial_error_mitigation.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,35 @@
4949
Mitigating noise in a simple circuit
5050
------------------------------------
5151
52-
We first need a noisy device to execute our circuit on. Let's keep things simple for now by loading
53-
the :mod:`default.mixed <pennylane.devices.default_mixed>` simulator and artificially adding
54-
:class:`PhaseDamping <pennylane.PhaseDamping>` noise.
52+
We first need a noisy device to execute our circuit on. Let's keep things simple
53+
for now by loading the :mod:`default.mixed <pennylane.devices.default_mixed>` simulator
54+
and artificially adding :class:`PhaseDamping <pennylane.PhaseDamping>` noise using a
55+
:class:`NoiseModel <pennylane.NoiseModel>`.
5556
"""
5657

5758
import pennylane as qml
5859

5960
n_wires = 4
6061

61-
# Describe noise
62-
noise_gate = qml.PhaseDamping
63-
noise_strength = 0.1
62+
# Describe noise model
63+
fcond = qml.noise.wires_in(range(n_wires))
64+
noise = qml.noise.partial_wires(qml.PhaseDamping, 0.1)
65+
noise_model = qml.NoiseModel({fcond: noise})
6466

6567
# Load devices
6668
dev_ideal = qml.device("default.mixed", wires=n_wires)
67-
dev_noisy = qml.transforms.insert(dev_ideal, noise_gate, noise_strength)
69+
dev_noisy = qml.add_noise(dev_ideal, noise_model=noise_model)
6870

6971
###############################################################################
70-
# In the above, we load a noise-free device ``dev_ideal`` and a noisy device ``dev_noisy``, which
71-
# is constructed from the :func:`qml.transforms.insert <pennylane.transforms.insert>` transform.
72-
# This transform works by intercepting each circuit executed on the device and adding the
73-
# :class:`PhaseDamping <pennylane.PhaseDamping>` noise channel directly after every gate in the
74-
# circuit. To get a better understanding of noise channels like
75-
# :class:`PhaseDamping <pennylane.PhaseDamping>`, check out the :doc:`tutorial_noisy_circuits`
76-
# tutorial.
72+
# In the above, we load a noise-free device ``dev_ideal`` and a noisy device ``dev_noisy``,
73+
# which is constructed from the :func:`qml.add_noise <pennylane.transforms.add_noise>`
74+
# transform. This transform works by intercepting each circuit executed on the device and
75+
# adding the noise to it based on the ``noise_model``. For example, in this case, it will
76+
# add :class:`PhaseDamping <pennylane.PhaseDamping>` noise channel after every gate in the
77+
# circuit acting on wires :math:`[0, 1, 2, 3]`. To get a better understanding of noise
78+
# channels like :class:`PhaseDamping <pennylane.PhaseDamping>` and using noise models,
79+
# check out the :doc:`tutorial_noisy_circuits` and :doc:`tutorial_how_to_use_noise_models`
80+
# tutorials, respectively.
7781
#
7882
# The next step is to define our circuit. Inspired by the mirror circuits concept introduced by
7983
# Proctor *et al.* [#proctor2020measuring]_ let's fix a circuit that applies a unitary :math:`U`
@@ -112,6 +116,7 @@ def circuit(w1, w2):
112116

113117
ideal_qnode = qml.QNode(circuit, dev_ideal)
114118
noisy_qnode = qml.QNode(circuit, dev_noisy)
119+
noisy_qnode = qml.transforms.decompose(noisy_qnode, gate_set = ["RY", "CZ"])
115120

116121
##############################################################################
117122
# First, we'll visualize the circuit:
@@ -490,6 +495,7 @@ def qchem_circuit(phi):
490495

491496
ideal_energy = qml.QNode(qchem_circuit, dev_ideal)
492497
noisy_energy = qml.QNode(qchem_circuit, dev_noisy)
498+
noisy_energy = qml.transforms.decompose(noisy_energy, gate_set=["RX", "RY", "RZ", "CNOT"])
493499

494500
ideal_energies.append(ideal_energy(phi))
495501
noisy_energies.append(noisy_energy(phi))
@@ -517,6 +523,7 @@ def qchem_circuit(phi):
517523
qml.DoubleExcitation(phi, wires=range(n_wires)),
518524
]
519525
circuit = qml.tape.QuantumTape(ops)
526+
[circuit], _ = qml.transforms.decompose(circuit, gate_set=["RX", "RY", "RZ", "CNOT"])
520527

521528
# Define custom executor that expands Hamiltonian measurement
522529
# into a linear combination of tensor products of Pauli

0 commit comments

Comments
 (0)