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

Remove code being deprecated in v0.39 #1220

Merged
merged 17 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
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;\
mudit2812 marked this conversation as resolved.
Show resolved Hide resolved
$$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
)
mudit2812 marked this conversation as resolved.
Show resolved Hide resolved

# 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
mudit2812 marked this conversation as resolved.
Show resolved Hide resolved
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
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")
astralcai marked this conversation as resolved.
Show resolved Hide resolved
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
24 changes: 12 additions & 12 deletions demonstrations/tutorial_sc_qubits.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@
# angle :math:`\phi` actually means a rotation by :math:`\omega_r+\phi.` In PennyLane, the operations read:

import pennylane as qml
from pennylane import numpy as np
import numpy as np
import matplotlib.pyplot as plt

# Call the default.gaussian device with 50 shots
Expand All @@ -416,14 +416,14 @@
# Implement displacement and rotation and measure both X and P observables


@qml.qnode(dev, interface="autograd")
@qml.qnode(dev)
def measure_P_shots(time, state):
qml.Displacement(epsilon * time, 0, wires=0)
qml.Rotation((-1) ** state * chi * time, wires=0)
return qml.sample(qml.QuadP(0))


@qml.qnode(dev, interface="autograd")
@qml.qnode(dev)
def measure_X_shots(time, state):
qml.Displacement(epsilon * time, 0, wires=0)
qml.Rotation((-1) ** state * chi * time, wires=0)
Expand Down Expand Up @@ -499,7 +499,7 @@ def measure_X_shots(time, state):
dev2 = qml.device("lightning.qubit", wires=1)

# Implement Hamiltonian evolution given phase phi and time t, from a given initial state
@qml.qnode(dev2, interface="autograd")
@qml.qnode(dev2)
def H_evolve(state, phi, time):

if state == 1:
Expand All @@ -513,7 +513,7 @@ def H_evolve(state, phi, time):


# Implement X rotation exactly
@qml.qnode(dev2, interface="autograd")
@qml.qnode(dev2)
def Sc_X_rot(state, phi):

if state == 1:
Expand All @@ -524,7 +524,7 @@ def Sc_X_rot(state, phi):


# Implement Y rotation exactly
@qml.qnode(dev2, interface="autograd")
@qml.qnode(dev2)
def Sc_Y_rot(state, phi):

if state == 1:
Expand Down Expand Up @@ -617,17 +617,17 @@ def Sc_Y_rot(state, phi):
Two_qubit_H = qml.Hamiltonian(coeffs, ops)

# Implement Hamiltonian evolution for time t and some initial computational basis state
@qml.qnode(dev3, interface="autograd")
@qml.qnode(dev3)
def Sc_ISWAP(basis_state, time):
qml.templates.BasisStatePreparation(basis_state, wires=range(2))
qml.BasisState(basis_state, wires=range(2))
ApproxTimeEvolution(Two_qubit_H, time, 1)
return qml.state()


# Implement ISWAP exactly
@qml.qnode(dev3, interface="autograd")
@qml.qnode(dev3)
def iswap(basis_state):
qml.templates.BasisStatePreparation(basis_state, wires=range(2))
qml.BasisState(basis_state, wires=range(2))
qml.ISWAP(wires=[0, 1])
return qml.state()

Expand Down Expand Up @@ -732,10 +732,10 @@ def cnot_with_iswap():
# the evolution under this Hamiltonian for a time :math:`t=\tfrac{\pi}{4\Omega}` with :math:`R_x` and :math:`R_y` rotations
# and a ``qml.Hadamard`` gate:
#
@qml.qnode(dev3, interface="autograd")
@qml.qnode(dev3)
def H_evolve(state, phi, time):
# Prepare initial state
qml.templates.BasisStatePreparation(state, wires=range(2))
qml.BasisState(state, wires=range(2))
# Define Hamiltonian
coeffs = [np.cos(phi), np.sin(phi)]
ops = [qml.PauliZ(0) @ qml.PauliX(1), qml.PauliZ(0) @ qml.PauliY(1)]
Expand Down
Loading
Loading