From 3d02a26d1c2dd02ec8927646dc221793da4907c4 Mon Sep 17 00:00:00 2001 From: Mudit Pandey Date: Thu, 3 Oct 2024 17:06:21 -0400 Subject: [PATCH] Remove code being deprecated in v0.39 (#1220) [sc-73935] * Remove deprecated code from demos. * Update various `from pennylane import numpy as np` to `import numpy as np`. --- Makefile | 2 +- conf.py | 3 ++ .../tutorial_backprop.metadata.json | 2 +- demonstrations/tutorial_backprop.py | 35 ++++++++++++------- demonstrations/tutorial_bluequbit.py | 4 +++ .../tutorial_kernels_module.metadata.json | 2 +- demonstrations/tutorial_kernels_module.py | 4 ++- ...al_learning_from_experiments.metadata.json | 2 +- .../tutorial_learning_from_experiments.py | 24 ++++++------- ...utorial_local_cost_functions.metadata.json | 2 +- .../tutorial_local_cost_functions.py | 9 +++-- .../tutorial_mcm_introduction.metadata.json | 2 +- demonstrations/tutorial_mcm_introduction.py | 2 +- .../tutorial_rl_pulse.metadata.json | 2 +- demonstrations/tutorial_rl_pulse.py | 4 +-- .../tutorial_sc_qubits.metadata.json | 2 +- demonstrations/tutorial_sc_qubits.py | 24 ++++++------- .../tutorial_tn_circuits.metadata.json | 2 +- demonstrations/tutorial_tn_circuits.py | 13 +++---- .../tutorial_trapped_ions.metadata.json | 2 +- demonstrations/tutorial_trapped_ions.py | 22 ++++++------ demonstrations/tutorial_vqt.metadata.json | 2 +- demonstrations/tutorial_vqt.py | 18 +++++----- 23 files changed, 103 insertions(+), 81 deletions(-) diff --git a/Makefile b/Makefile index 76860ac0ac..ff5c334968 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/conf.py b/conf.py index c8a9239f6e..4e105066e1 100644 --- a/conf.py +++ b/conf.py @@ -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"] diff --git a/demonstrations/tutorial_backprop.metadata.json b/demonstrations/tutorial_backprop.metadata.json index b472049cb5..fa7b27bdda 100644 --- a/demonstrations/tutorial_backprop.metadata.json +++ b/demonstrations/tutorial_backprop.metadata.json @@ -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" ], diff --git a/demonstrations/tutorial_backprop.py b/demonstrations/tutorial_backprop.py index 6b33f67f6b..469cb1d5ca 100644 --- a/demonstrations/tutorial_backprop.py +++ b/demonstrations/tutorial_backprop.py @@ -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 @@ -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)) @@ -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) @@ -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)) @@ -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 ` 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 @@ -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. @@ -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}") diff --git a/demonstrations/tutorial_bluequbit.py b/demonstrations/tutorial_bluequbit.py index 95f74963e4..574356d8ac 100644 --- a/demonstrations/tutorial_bluequbit.py +++ b/demonstrations/tutorial_bluequbit.py @@ -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) diff --git a/demonstrations/tutorial_kernels_module.metadata.json b/demonstrations/tutorial_kernels_module.metadata.json index 71eb88b6e3..383283de05 100644 --- a/demonstrations/tutorial_kernels_module.metadata.json +++ b/demonstrations/tutorial_kernels_module.metadata.json @@ -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" ], diff --git a/demonstrations/tutorial_kernels_module.py b/demonstrations/tutorial_kernels_module.py index e1b5d16c76..8f8ea23d12 100644 --- a/demonstrations/tutorial_kernels_module.py +++ b/demonstrations/tutorial_kernels_module.py @@ -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]) ############################################################################## diff --git a/demonstrations/tutorial_learning_from_experiments.metadata.json b/demonstrations/tutorial_learning_from_experiments.metadata.json index 0c4563082e..3df2006331 100644 --- a/demonstrations/tutorial_learning_from_experiments.metadata.json +++ b/demonstrations/tutorial_learning_from_experiments.metadata.json @@ -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" ], diff --git a/demonstrations/tutorial_learning_from_experiments.py b/demonstrations/tutorial_learning_from_experiments.py index 1ab73b9589..47ac16b729 100644 --- a/demonstrations/tutorial_learning_from_experiments.py +++ b/demonstrations/tutorial_learning_from_experiments.py @@ -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) @@ -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" @@ -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) @@ -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) diff --git a/demonstrations/tutorial_local_cost_functions.metadata.json b/demonstrations/tutorial_local_cost_functions.metadata.json index 91b2ec37d6..1f1e2ffcc7 100644 --- a/demonstrations/tutorial_local_cost_functions.metadata.json +++ b/demonstrations/tutorial_local_cost_functions.metadata.json @@ -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" ], diff --git a/demonstrations/tutorial_local_cost_functions.py b/demonstrations/tutorial_local_cost_functions.py index 959df983aa..f981a9f485 100644 --- a/demonstrations/tutorial_local_cost_functions.py +++ b/demonstrations/tutorial_local_cost_functions.py @@ -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") @@ -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): diff --git a/demonstrations/tutorial_mcm_introduction.metadata.json b/demonstrations/tutorial_mcm_introduction.metadata.json index 586633d57e..6aacafa406 100644 --- a/demonstrations/tutorial_mcm_introduction.metadata.json +++ b/demonstrations/tutorial_mcm_introduction.metadata.json @@ -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" diff --git a/demonstrations/tutorial_mcm_introduction.py b/demonstrations/tutorial_mcm_introduction.py index ccac69f82d..39c887fd35 100644 --- a/demonstrations/tutorial_mcm_introduction.py +++ b/demonstrations/tutorial_mcm_introduction.py @@ -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 diff --git a/demonstrations/tutorial_rl_pulse.metadata.json b/demonstrations/tutorial_rl_pulse.metadata.json index 0e59a897ba..1dd4af105f 100644 --- a/demonstrations/tutorial_rl_pulse.metadata.json +++ b/demonstrations/tutorial_rl_pulse.metadata.json @@ -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", diff --git a/demonstrations/tutorial_rl_pulse.py b/demonstrations/tutorial_rl_pulse.py index 08316f432a..7fbc9eab20 100644 --- a/demonstrations/tutorial_rl_pulse.py +++ b/demonstrations/tutorial_rl_pulse.py @@ -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() @@ -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) diff --git a/demonstrations/tutorial_sc_qubits.metadata.json b/demonstrations/tutorial_sc_qubits.metadata.json index 96bd72975d..29137dd18d 100644 --- a/demonstrations/tutorial_sc_qubits.metadata.json +++ b/demonstrations/tutorial_sc_qubits.metadata.json @@ -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" diff --git a/demonstrations/tutorial_sc_qubits.py b/demonstrations/tutorial_sc_qubits.py index f1f1fd8da5..9de73a5a3d 100644 --- a/demonstrations/tutorial_sc_qubits.py +++ b/demonstrations/tutorial_sc_qubits.py @@ -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 @@ -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) @@ -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: @@ -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: @@ -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: @@ -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() @@ -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)] diff --git a/demonstrations/tutorial_tn_circuits.metadata.json b/demonstrations/tutorial_tn_circuits.metadata.json index f0dde1deb3..5d33fa07cf 100644 --- a/demonstrations/tutorial_tn_circuits.metadata.json +++ b/demonstrations/tutorial_tn_circuits.metadata.json @@ -15,7 +15,7 @@ } ], "dateOfPublication": "2022-03-29T00:00:00+00:00", - "dateOfLastModification": "2024-08-12T00:00:00+00:00", + "dateOfLastModification": "2024-09-19T00:00:00+00:00", "categories": [ "Quantum Machine Learning" ], diff --git a/demonstrations/tutorial_tn_circuits.py b/demonstrations/tutorial_tn_circuits.py index 03e083f3f2..e317690f13 100644 --- a/demonstrations/tutorial_tn_circuits.py +++ b/demonstrations/tutorial_tn_circuits.py @@ -116,6 +116,7 @@ of tensors in the network. """ +import numpy as onp import pennylane as qml from pennylane import numpy as np @@ -232,8 +233,8 @@ def circuit(template_weights): shapes = qml.SimplifiedTwoDesign.shape(n_layers=1, n_wires=4) -weights = [np.random.random(size=shape) for shape in shapes] -template_weights = qml._np.array([weights] * 3, dtype="object") +weights = [onp.random.random(size=shape) for shape in shapes] +template_weights = onp.array([weights] * 3, dtype="object") fig, ax = qml.draw_mpl(circuit, level="device")(template_weights) ############################################################################## @@ -317,11 +318,11 @@ def block(weights, wires): ############################################################################## # As for the tensor-network architecture, we use the tree tensor-network quantum circuit. -# We use :class:`~pennylane.BasisStatePreparation` to encode the input images. -# The following code implements the :class:`~pennylane.BasisStatePreparation` encoding, +# We use :class:`~pennylane.BasisState` to encode the input images. +# The following code implements the :class:`~pennylane.BasisState` encoding, # followed by a :class:`~pennylane.TTN` circuit using the above ``block``. Finally, we compute the expectation # value of a :class:`~pennylane.PauliZ` measurement as the output. -# The circuit diagram below shows the full circuit. The :class:`~pennylane.BasisStatePreparation` +# The circuit diagram below shows the full circuit. The :class:`~pennylane.BasisState` # encoding appears in the initial :class:`~pennylane.PauliX` gates. dev = qml.device("default.qubit", wires=4) @@ -329,7 +330,7 @@ def block(weights, wires): @qml.qnode(dev) def circuit(image, template_weights): - qml.BasisStatePreparation(image, wires=range(4)) + qml.BasisState(image, wires=range(4)) qml.TTN( wires=range(4), n_block_wires=2, diff --git a/demonstrations/tutorial_trapped_ions.metadata.json b/demonstrations/tutorial_trapped_ions.metadata.json index 977922539a..40e477bcc7 100644 --- a/demonstrations/tutorial_trapped_ions.metadata.json +++ b/demonstrations/tutorial_trapped_ions.metadata.json @@ -6,7 +6,7 @@ } ], "dateOfPublication": "2021-11-10T00: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" diff --git a/demonstrations/tutorial_trapped_ions.py b/demonstrations/tutorial_trapped_ions.py index 778eb1d512..507d970bdc 100644 --- a/demonstrations/tutorial_trapped_ions.py +++ b/demonstrations/tutorial_trapped_ions.py @@ -401,7 +401,7 @@ # duration :math:`t` of the pulse, with :math:`\Omega` set to 100 kHz. import pennylane as qml -from pennylane import numpy as np +import numpy as np from scipy.linalg import expm Omega = 100 @@ -422,7 +422,7 @@ def evolution(phi, t): dev = qml.device("default.qubit", wires=1) -@qml.qnode(dev, interface="autograd") +@qml.qnode(dev) def ion_hadamard(state): if state == 1: @@ -440,7 +440,7 @@ def ion_hadamard(state): return qml.state() #For comparison, we use the Hadamard built into PennyLane -@qml.qnode(dev, interface="autograd") +@qml.qnode(dev) def hadamard(state): if state == 1: @@ -459,7 +459,7 @@ def hadamard(state): # A similar exercise can be done for the :math:`T` gate: -@qml.qnode(dev, interface="autograd") +@qml.qnode(dev) def ion_Tgate(state): if state == 1: @@ -472,7 +472,7 @@ def ion_Tgate(state): return qml.state() -@qml.qnode(dev, interface="autograd") +@qml.qnode(dev) def tgate(state): if state == 1: @@ -504,7 +504,7 @@ def tgate(state): import matplotlib.pyplot as plt -@qml.qnode(dev, interface="autograd") +@qml.qnode(dev) def evolution_prob(t): qml.QubitUnitary(evolution(0, t / Omega), wires=0) @@ -513,7 +513,7 @@ def evolution_prob(t): t = np.linspace(0, 4 * np.pi, 101) -s = [evolution_prob(i)[1].numpy() for i in t] +s = [evolution_prob(i)[1] for i in t] fig1, ax1 = plt.subplots(figsize=(9, 6)) @@ -811,11 +811,11 @@ def Molmer_Sorensen(t): dev2 = qml.device("default.qubit",wires=2) -@qml.qnode(dev2, interface="autograd") +@qml.qnode(dev2) def ion_cnot(basis_state): #Prepare the two-qubit basis states from the input - qml.templates.BasisStatePreparation(basis_state, wires=range(2)) + qml.BasisState(basis_state, wires=range(2)) #Implements the circuit shown above qml.RY(np.pi/2, wires=0) @@ -827,10 +827,10 @@ def ion_cnot(basis_state): return qml.state() #Compare with built-in CNOT -@qml.qnode(dev2, interface="autograd") +@qml.qnode(dev2) def cnot_gate(basis_state): - qml.templates.BasisStatePreparation(basis_state, wires=range(2)) + qml.BasisState(basis_state, wires=range(2)) qml.CNOT(wires=[0,1]) diff --git a/demonstrations/tutorial_vqt.metadata.json b/demonstrations/tutorial_vqt.metadata.json index 7f5b69076d..567a2b9dc7 100644 --- a/demonstrations/tutorial_vqt.metadata.json +++ b/demonstrations/tutorial_vqt.metadata.json @@ -6,7 +6,7 @@ } ], "dateOfPublication": "2020-07-07T00:00:00+00:00", - "dateOfLastModification": "2024-08-05T00:00:00+00:00", + "dateOfLastModification": "2024-09-19T00:00:00+00:00", "categories": [ "Optimization" ], diff --git a/demonstrations/tutorial_vqt.py b/demonstrations/tutorial_vqt.py index 3b3b324994..4ffce13164 100644 --- a/demonstrations/tutorial_vqt.py +++ b/demonstrations/tutorial_vqt.py @@ -110,7 +110,6 @@ import pennylane as qml from matplotlib import pyplot as plt import numpy as np -from numpy import array import scipy from scipy.optimize import minimize import networkx as nx @@ -250,6 +249,14 @@ def single_rotation(phi_params, qubits): # +def CRX_ring(parameters, wires): + """Apply a controlled RX on all wires in a ring pattern""" + n_wires = len(wires) + + for param, w in zip(parameters, wires): + qml.CRX(param, wires=[w % n_wires, (w + 1) % n_wires]) + + depth = 4 dev = qml.device("lightning.qubit", wires=nr_qubits) @@ -257,17 +264,12 @@ def single_rotation(phi_params, qubits): def quantum_circuit(rotation_params, coupling_params, sample=None, return_state=False): # Prepares the initial basis state corresponding to the sample - qml.BasisStatePreparation(sample, wires=range(nr_qubits)) + qml.BasisState(sample, wires=range(nr_qubits)) # Prepares the variational ansatz for the circuit for i in range(0, depth): single_rotation(rotation_params[i], range(nr_qubits)) - qml.broadcast( - unitary=qml.CRX, - pattern="ring", - wires=range(nr_qubits), - parameters=coupling_params[i], - ) + CRX_ring(coupling_params[i], list(range(nr_qubits))) if return_state: return qml.state()