Skip to content

Commit

Permalink
Remove code being deprecated in v0.39 (#1220)
Browse files Browse the repository at this point in the history
[sc-73935]

* Remove deprecated code from demos.
* Update various `from pennylane import numpy as np` to `import numpy as
np`.
  • Loading branch information
mudit2812 authored Oct 3, 2024
1 parent f22b7a7 commit 3d02a26
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ environment:
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane-cirq.git#egg=pennylane-cirq;\
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane-qiskit.git#egg=pennylane-qiskit;\
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane-qulacs.git#egg=pennylane-qulacs;\
$$PYTHON_VENV_PATH/bin/python -m pip install --extra-index-url https://test.pypi.org/simple/ PennyLane-Lightning --pre --upgrade;\
$$PYTHON_VENV_PATH/bin/python -m pip install --extra-index-url https://test.pypi.org/simple/ PennyLane-Catalyst --pre --upgrade;\
$$PYTHON_VENV_PATH/bin/python -m pip install --extra-index-url https://test.pypi.org/simple/ PennyLane-Lightning --pre --upgrade;\
$$PYTHON_VENV_PATH/bin/python -m pip install --upgrade git+https://github.com/PennyLaneAI/pennylane.git#egg=pennylane;\
fi;\
fi
3 changes: 3 additions & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@

# Raise PennyLane deprecation warnings as errors
warnings.filterwarnings("error", category=PennyLaneDeprecationWarning)
warnings.filterwarnings(
"ignore", message="Device will no longer be accessible", category=PennyLaneDeprecationWarning
)

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_backprop.metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
],
"dateOfPublication": "2020-08-11T00:00:00+00:00",
"dateOfLastModification": "2024-08-06T00:00:00+00:00",
"dateOfLastModification": "2024-09-19T00:00:00+00:00",
"categories": [
"Getting Started"
],
Expand Down
35 changes: 22 additions & 13 deletions demonstrations/tutorial_backprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
Let's have a go implementing the parameter-shift rule manually in PennyLane.
"""
import pennylane as qml
from jax import numpy as np
from jax import numpy as jnp
from matplotlib import pyplot as plt
import jax

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


def CNOT_ring(wires):
"""Apply CNOTs in a ring pattern"""
n_wires = len(wires)

for w in wires:
qml.CNOT([w % n_wires, (w + 1) % n_wires])


@qml.qnode(dev, diff_method="parameter-shift")
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
qml.RZ(params[2], wires=2)

qml.broadcast(qml.CNOT, wires=[0, 1, 2], pattern="ring")
CNOT_ring(wires=[0, 1, 2])

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

qml.broadcast(qml.CNOT, wires=[0, 1, 2], pattern="ring")
CNOT_ring(wires=[0, 1, 2])
return qml.expval(qml.PauliY(0) @ qml.PauliZ(2))


Expand Down Expand Up @@ -109,10 +118,10 @@ def circuit(params):

def parameter_shift_term(qnode, params, i):
shifted = params.copy()
shifted = shifted.at[i].add(np.pi/2)
shifted = shifted.at[i].add(jnp.pi/2)
forward = qnode(shifted) # forward evaluation

shifted = shifted.at[i].add(-np.pi)
shifted = shifted.at[i].add(-jnp.pi)
backward = qnode(shifted) # backward evaluation

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

def parameter_shift(qnode, params):
gradients = np.zeros([len(params)])
gradients = jnp.zeros([len(params)])

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

print(np.stack(qml.gradients.param_shift(circuit)(params)))
print(jnp.stack(qml.gradients.param_shift(circuit)(params)))

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

gradient_shift = np.array(gradient_shift).T
gradient_backprop = np.array(gradient_backprop).T
forward_shift = np.array(forward_shift).T
forward_backprop = np.array(forward_backprop).T
gradient_shift = jnp.array(gradient_shift).T
gradient_backprop = jnp.array(gradient_backprop).T
forward_shift = jnp.array(forward_shift).T
forward_backprop = jnp.array(forward_backprop).T

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

ax.plot(x, m_shift * x + c_shift, '--', label=f"{m_shift:.2f}p{c_shift:+.2f}")
ax.plot(x, m_back * x + c_back, '--', label=f"{m_back:.2f}p{c_back:+.2f}")
Expand Down
4 changes: 4 additions & 0 deletions demonstrations/tutorial_bluequbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
import matplotlib.pyplot as plt
import numpy as np

# This filter will suppress deprecation warnings for viewability
import warnings
warnings.filterwarnings("ignore", "QubitDevice", qml.PennyLaneDeprecationWarning)


def bell_pair():
qml.Hadamard(0)
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_kernels_module.metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"dateOfPublication": "2021-06-24T00:00:00+00:00",
"dateOfLastModification": "2024-08-05T00:00:00+00:00",
"dateOfLastModification": "2024-09-19T00:00:00+00:00",
"categories": [
"Quantum Machine Learning"
],
Expand Down
4 changes: 3 additions & 1 deletion demonstrations/tutorial_kernels_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ def layer(x, params, wires, i0=0, inc=1):
i += inc
qml.RY(params[0, j], wires=[wire])

qml.broadcast(unitary=qml.CRZ, pattern="ring", wires=wires, parameters=params[1])
n_wires = len(wires)
for p, w in zip(params[1], wires):
qml.CRZ(p, wires=[w % n_wires, (w + 1) % n_wires])


##############################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
],
"dateOfPublication": "2022-04-18T00:00:00+00:00",
"dateOfLastModification": "2024-08-06T00:00:00+00:00",
"dateOfLastModification": "2024-09-19T00:00:00+00:00",
"categories": [
"Quantum Machine Learning"
],
Expand Down
24 changes: 11 additions & 13 deletions demonstrations/tutorial_learning_from_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@

import pennylane as qml
from pennylane.templates.layers import RandomLayers
from pennylane import numpy as np
import numpy as np

np.random.seed(234087)

Expand Down Expand Up @@ -346,6 +346,12 @@ def process_data(raw_data):
dev = qml.device("lightning.qubit", wires=qubits * 2, shots=n_shots)


def CNOT_sequence(control_wires, target_wires):
"""Apply CNOTs in sequence using the provided control and target wires"""
for c_wire, t_wire in zip(control_wires, target_wires):
qml.CNOT([c_wire, t_wire])


@qml.qnode(dev)
def enhanced_circuit(ts=False):
"implement the enhanced circuit, using a random unitary"
Expand All @@ -361,14 +367,10 @@ def enhanced_circuit(ts=False):
for q in range(qubits):
qml.Hadamard(wires=q)

qml.broadcast(
qml.CNOT, pattern=[[q, qubits + q] for q in range(qubits)], wires=range(qubits * 2)
)
CNOT_sequence(control_wires=range(qubits), target_wires=range(qubits, 2 * qubits))
RandomLayers(weights, wires=range(0, qubits), rotations=ops, seed=seed)
RandomLayers(weights, wires=range(qubits, 2 * qubits), rotations=ops, seed=seed)
qml.broadcast(
qml.CNOT, pattern=[[q, qubits + q] for q in range(qubits)], wires=range(qubits * 2)
)
CNOT_sequence(control_wires=range(qubits), target_wires=range(qubits, 2 * qubits))

for q in range(qubits):
qml.Hadamard(wires=q)
Expand Down Expand Up @@ -479,15 +481,11 @@ def enhanced_circuit(ts=False):
for q in range(qubits):
qml.Hadamard(wires=q)

qml.broadcast(
qml.CNOT, pattern=[[q, qubits + q] for q in range(qubits)], wires=range(qubits * 2)
)
CNOT_sequence(control_wires=range(qubits), target_wires=range(qubits, 2 * qubits))
RandomLayers(weights, wires=range(0, qubits), rotations=ops, seed=seed)
RandomLayers(weights, wires=range(qubits, 2 * qubits), rotations=ops, seed=seed)
noise_layer(np.pi / 4) # added noise layer
qml.broadcast(
qml.CNOT, pattern=[[qubits + q, q] for q in range(qubits)], wires=range(qubits * 2)
)
CNOT_sequence(control_wires=range(qubits, 2 * qubits), target_wires=range(qubits))

for q in range(qubits):
qml.Hadamard(wires=qubits + q)
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_local_cost_functions.metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
],
"dateOfPublication": "2020-09-09T00:00:00+00:00",
"dateOfLastModification": "2024-08-05T00:00:00+00:00",
"dateOfLastModification": "2024-09-19T00:00:00+00:00",
"categories": [
"Optimization"
],
Expand Down
9 changes: 6 additions & 3 deletions demonstrations/tutorial_local_cost_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,16 @@ def global_cost_simple(rotations):
for i in range(wires):
qml.RX(rotations[0][i], wires=i)
qml.RY(rotations[1][i], wires=i)
qml.broadcast(qml.CNOT, wires=range(wires), pattern="chain")
for i in range(wires - 1):
qml.CNOT([i, i + 1])
return qml.probs(wires=range(wires))

def local_cost_simple(rotations):
for i in range(wires):
qml.RX(rotations[0][i], wires=i)
qml.RY(rotations[1][i], wires=i)
qml.broadcast(qml.CNOT, wires=range(wires), pattern="chain")
for i in range(wires - 1):
qml.CNOT([i, i + 1])
return qml.probs(wires=[0])

global_circuit = qml.QNode(global_cost_simple, dev, interface="autograd")
Expand Down Expand Up @@ -371,7 +373,8 @@ def tunable_cost_simple(rotations):
for i in range(wires):
qml.RX(rotations[0][i], wires=i)
qml.RY(rotations[1][i], wires=i)
qml.broadcast(qml.CNOT, wires=range(wires), pattern="chain")
for i in range(wires - 1):
qml.CNOT([i, i + 1])
return qml.probs(range(locality))

def cost_tunable(rotations):
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_mcm_introduction.metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
],
"dateOfPublication": "2024-05-10T00:00:00+00:00",
"dateOfLastModification": "2024-08-05T00:00:00+00:00",
"dateOfLastModification": "2024-09-19T00:00:00+00:00",
"categories": [
"Getting Started",
"Quantum Computing"
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_mcm_introduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def bell_pair_with_reset(reset):
magic_state = np.array([1, np.exp(1j * np.pi / 4)]) / np.sqrt(2)

def t_gadget(wire, aux_wire):
qml.QubitStateVector(magic_state, aux_wire)
qml.StatePrep(magic_state, aux_wire)
qml.CNOT([wire, aux_wire])
mcm = qml.measure(aux_wire, reset=True) # Resetting disentangles aux qubit
qml.cond(mcm, qml.S)(wire) # Apply qml.S(wire) if mcm was 1
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_rl_pulse.metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
],
"dateOfPublication": "2024-04-09T00:00:00+00:00",
"dateOfLastModification": "2024-09-04T00:00:00+00:00",
"dateOfLastModification": "2024-09-19T00:00:00+00:00",
"categories": [
"Getting Started",
"Optimization",
Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_rl_pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
@partial(jax.vmap, in_axes=(0, None, 0, None))
@qml.qnode(device=device, interface="jax")
def evolve_states(state, H, params, t):
qml.QubitStateVector(state, wires=wires)
qml.StatePrep(state, wires=wires)
qml.evolve(H)(params, t, atol=1e-5)
return qml.state()

Expand Down Expand Up @@ -929,7 +929,7 @@ def get_drive(timespan, freq, wire):
@qml.qnode(device=device, interface="jax")
def evolve_states(state, params, t):
params_sq, params_cr = params
qml.QubitStateVector(state, wires=wires)
qml.StatePrep(state, wires=wires)
# Single qubit pulses
qml.evolve(H_int + H_sq_ini)(params_sq, t, atol=1e-5)

Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_sc_qubits.metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
],
"dateOfPublication": "2022-03-22T00:00:00+00:00",
"dateOfLastModification": "2024-08-06T00:00:00+00:00",
"dateOfLastModification": "2024-09-19T00:00:00+00:00",
"categories": [
"Quantum Hardware",
"Quantum Computing"
Expand Down
Loading

0 comments on commit 3d02a26

Please sign in to comment.