Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
75 changes: 75 additions & 0 deletions change_qml_qp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os
import re
import sys

# Configuration
LIMIT = 500
changes_made = 0
modified_dirs = set() # Tracks unique directories that had modifications

repo_path = './demonstrations_v2'

# Regex patterns
import_pattern = re.compile(r'import\s+pennylane\s+as\s+qml\b')
alias_pattern = re.compile(r'\bqml\.')

for root, dirs, files in os.walk(repo_path):
# Sort directories and files in-place so the traversal is alphabetical
dirs.sort()
files.sort()

# Optional: skip hidden directories like .git or virtual environments
if '.git' in root or 'venv' in root:
continue

for file in files:
if changes_made >= LIMIT:
break

if file.endswith('.py'):
filepath = os.path.join(root, file)

with open(filepath, 'r', encoding='utf-8') as f:
Comment thread
JerryChen97 marked this conversation as resolved.
Outdated
lines = f.readlines()

new_lines = []
file_modified = False

for line in lines:
original_line = line

# Apply both regex replacements
line = import_pattern.sub('import pennylane as qp', line)
line = alias_pattern.sub('qp.', line)

new_lines.append(line)

if original_line != line:
file_modified = True
changes_made += 1

if changes_made >= LIMIT:
# If limit reached mid-file, keep the rest of the file unchanged
new_lines.extend(lines[len(new_lines):])
break

if file_modified:
with open(filepath, 'w', encoding='utf-8') as f:
Comment thread
JerryChen97 marked this conversation as resolved.
Outdated
f.writelines(new_lines)

# Add the current directory to our set of modified directories
modified_dirs.add(root)

if changes_made >= LIMIT:
print(f"Reached limit of {LIMIT} line changes. Stopping.")
break

print(f"\nRefactoring complete. Total lines changed: {changes_made}")

# Print the summary of modified subdirectories
if modified_dirs:
print("Files were modified in the following directories:")
for directory in sorted(modified_dirs):
print(f" - {directory}")
else:
print("No directories were modified (no matches found).")
36 changes: 18 additions & 18 deletions demonstrations_v2/adjoint_diff_benchmarking/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import timeit
import matplotlib.pyplot as plt
import pennylane as qml
import pennylane as qp
import pennylane.numpy as pnp

plt.style.use("bmh")
Expand All @@ -27,7 +27,7 @@


def get_time(qnode, params):
globals_dict = {"grad": qml.grad, "circuit": qnode, "params": params}
globals_dict = {"grad": qp.grad, "circuit": qnode, "params": params}
return timeit.timeit("grad(circuit)(params)", globals=globals_dict, number=n_samples)


Expand All @@ -39,19 +39,19 @@ def wires_scaling(n_wires, n_layers):
t_backprop = []

def circuit(params, wires):
qml.StronglyEntanglingLayers(params, wires=range(wires))
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2))
qp.StronglyEntanglingLayers(params, wires=range(wires))
return qp.expval(qp.PauliZ(0) @ qp.PauliZ(1) @ qp.PauliZ(2))

for i_wires in n_wires:
dev = qml.device("lightning.qubit", wires=i_wires)
dev_python = qml.device("default.qubit", wires=i_wires)
dev = qp.device("lightning.qubit", wires=i_wires)
dev_python = qp.device("default.qubit", wires=i_wires)

circuit_adjoint = qml.QNode(lambda x: circuit(x, wires=i_wires), dev, diff_method="adjoint")
circuit_ps = qml.QNode(lambda x: circuit(x, wires=i_wires), dev, diff_method="parameter-shift")
circuit_backprop = qml.QNode(lambda x: circuit(x, wires=i_wires), dev_python, diff_method="backprop")
circuit_adjoint = qp.QNode(lambda x: circuit(x, wires=i_wires), dev, diff_method="adjoint")
circuit_ps = qp.QNode(lambda x: circuit(x, wires=i_wires), dev, diff_method="parameter-shift")
circuit_backprop = qp.QNode(lambda x: circuit(x, wires=i_wires), dev_python, diff_method="backprop")

# set up the parameters
param_shape = qml.StronglyEntanglingLayers.shape(n_wires=i_wires, n_layers=n_layers)
param_shape = qp.StronglyEntanglingLayers.shape(n_wires=i_wires, n_layers=n_layers)
params = rng.normal(size=pnp.prod(param_shape), requires_grad=True).reshape(param_shape)

t_adjoint.append(get_time(circuit_adjoint, params))
Expand All @@ -64,24 +64,24 @@ def circuit(params, wires):
def layers_scaling(n_wires, n_layers):
rng = pnp.random.default_rng(12345)

dev = qml.device("lightning.qubit", wires=n_wires)
dev_python = qml.device("default.qubit", wires=n_wires)
dev = qp.device("lightning.qubit", wires=n_wires)
dev_python = qp.device("default.qubit", wires=n_wires)

t_adjoint = []
t_ps = []
t_backprop = []

def circuit(params):
qml.StronglyEntanglingLayers(params, wires=range(n_wires))
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2))
qp.StronglyEntanglingLayers(params, wires=range(n_wires))
return qp.expval(qp.PauliZ(0) @ qp.PauliZ(1) @ qp.PauliZ(2))

circuit_adjoint = qml.QNode(circuit, dev, diff_method="adjoint")
circuit_ps = qml.QNode(circuit, dev, diff_method="parameter-shift")
circuit_backprop = qml.QNode(circuit, dev_python, diff_method="backprop")
circuit_adjoint = qp.QNode(circuit, dev, diff_method="adjoint")
circuit_ps = qp.QNode(circuit, dev, diff_method="parameter-shift")
circuit_backprop = qp.QNode(circuit, dev_python, diff_method="backprop")

for i_layers in n_layers:
# set up the parameters
param_shape = qml.StronglyEntanglingLayers.shape(n_wires=n_wires, n_layers=i_layers)
param_shape = qp.StronglyEntanglingLayers.shape(n_wires=n_wires, n_layers=i_layers)
params = rng.normal(size=pnp.prod(param_shape), requires_grad=True).reshape(param_shape)

t_adjoint.append(get_time(circuit_adjoint, params))
Expand Down
44 changes: 22 additions & 22 deletions demonstrations_v2/ahs_aquila/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@

"""

import pennylane as qml
import pennylane as qp

aquila = qml.device(
aquila = qp.device(
"braket.aws.ahs",
device_arn="arn:aws:braket:us-east-1::device/qpu/quera/Aquila",
wires=3,
Expand All @@ -229,7 +229,7 @@
# warnings.warn(
#

rydberg_simulator = qml.device("braket.local.ahs", wires=3)
rydberg_simulator = qp.device("braket.local.ahs", wires=3)

######################################################################
# Creating a Rydberg Hamiltonian
Expand Down Expand Up @@ -331,7 +331,7 @@
# .. math:: \hat{H} = \sum_{j=1}^{N-1}\sum_{k=j+1}^{N} \frac{C_6}{R^6_{jk}}\hat{n}_j\hat{n}_k
#

H_interaction = qml.pulse.rydberg_interaction(coordinates, **settings)
H_interaction = qp.pulse.rydberg_interaction(coordinates, **settings)

######################################################################
# Driving field
Expand Down Expand Up @@ -478,7 +478,7 @@ def gaussian_fn(p, t):
# We can then define our drive using via :func:`~pennylane.pulse.rydberg_drive`:
#

global_drive = qml.pulse.rydberg_drive(amplitude=gaussian_fn, phase=0, detuning=0, wires=[0, 1, 2])
global_drive = qp.pulse.rydberg_drive(amplitude=gaussian_fn, phase=0, detuning=0, wires=[0, 1, 2])

######################################################################
# With only amplitude as non-zero, the overall driven Hamiltonian in this case simplifies to:
Expand Down Expand Up @@ -520,14 +520,14 @@ def gaussian_fn(p, t):
params = [amplitude_params]
ts = [0.0, 1.75]

default_qubit = qml.device("default.qubit", wires=3)
default_qubit = qp.device("default.qubit", wires=3)


@qml.set_shots(1000)
@qml.qnode(default_qubit, interface="jax")
@qp.set_shots(1000)
@qp.qnode(default_qubit, interface="jax")
def circuit(parameters):
qml.evolve(global_drive)(parameters, ts)
return qml.counts()
qp.evolve(global_drive)(parameters, ts)
return qp.counts()


circuit(params)
Expand All @@ -551,12 +551,12 @@ def circuit(parameters):


def circuit(params):
qml.evolve(H_interaction + global_drive)(params, ts)
return qml.counts()
qp.evolve(H_interaction + global_drive)(params, ts)
return qp.counts()


circuit_qml = qml.set_shots(qml.QNode(circuit, default_qubit, interface="jax"), shots=1000)
circuit_ahs = qml.QNode(circuit, rydberg_simulator)
circuit_qml = qp.set_shots(qp.QNode(circuit, default_qubit, interface="jax"), shots=1000)
circuit_ahs = qp.QNode(circuit, rydberg_simulator)

print(f"PennyLane simulation: {circuit_qml(params)}")
print(f"AWS local simulation: {circuit_ahs(params)}")
Expand Down Expand Up @@ -644,8 +644,8 @@ def circuit(params):
# defining the window; we’ll use ``windows=[0.01, 1.749]``. Our modified global drive is then:
#

amp_fn = qml.pulse.rect(gaussian_fn, windows=[0.01, 1.749])
global_drive = qml.pulse.rydberg_drive(amplitude=amp_fn, phase=0, detuning=0, wires=[0, 1, 2])
amp_fn = qp.pulse.rect(gaussian_fn, windows=[0.01, 1.749])
global_drive = qp.pulse.rydberg_drive(amplitude=amp_fn, phase=0, detuning=0, wires=[0, 1, 2])

######################################################################
# At this point we could skip directly to defining a ``qnode`` using the ``aquila`` device and running our
Expand All @@ -656,7 +656,7 @@ def circuit(params):
# To do this, we create the operator we will be using in our circuit, and pass it to a method on the
# hardware device that creates an AHS program for upload:
#
op = qml.evolve(H_interaction + global_drive)(params, ts)
op = qp.evolve(H_interaction + global_drive)(params, ts)
ahs_program = aquila.create_ahs_program(op)

######################################################################
Expand Down Expand Up @@ -712,7 +712,7 @@ def circuit(params):
# values for plotting the function defined in PennyLane for amplitude
input_times = np.linspace(*ts, 1000)
input_amplitudes = [
qml.pulse.rect(gaussian_fn, windows=[0.01, 1.749])(amplitude_params, _t) for _t in input_times
qp.pulse.rect(gaussian_fn, windows=[0.01, 1.749])(amplitude_params, _t) for _t in input_times
]

# plot PL input and hardware setpoints for comparison
Expand Down Expand Up @@ -754,11 +754,11 @@ def circuit(params):
#


# @qml.qnode(rydberg_simulator)
@qml.qnode(aquila)
# @qp.qnode(rydberg_simulator)
@qp.qnode(aquila)
def circuit(params):
qml.evolve(H_interaction + global_drive)(params, ts)
return qml.counts()
qp.evolve(H_interaction + global_drive)(params, ts)
return qp.counts()


circuit(params)
Expand Down
Loading
Loading