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

Update demos to remove warnings #705

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions demonstrations/tutorial_adaptive_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def circuit_1(params, excitations):
# with respect to the Hartree-Fock state.

dev = qml.device("default.qubit", wires=qubits)
cost_fn = qml.QNode(circuit_1, dev)
cost_fn = qml.QNode(circuit_1, dev, interface="autograd")

circuit_gradient = qml.grad(cost_fn, argnum=0)

Expand Down Expand Up @@ -181,7 +181,7 @@ def circuit_2(params, excitations, gates_select, params_select):
##############################################################################
# We now compute the gradients for the single excitation gates.

cost_fn = qml.QNode(circuit_2, dev)
cost_fn = qml.QNode(circuit_2, dev, interface="autograd")
circuit_gradient = qml.grad(cost_fn, argnum=0)
params = [0.0] * len(singles)

Expand Down Expand Up @@ -213,7 +213,7 @@ def circuit_2(params, excitations, gates_select, params_select):
# We perform one final step of optimization to get the ground-state energy. The resulting energy
# should match the exact energy of the ground electronic state of LiH which is -7.8825378193 Ha.

cost_fn = qml.QNode(circuit_1, dev)
cost_fn = qml.QNode(circuit_1, dev, interface="autograd")

params = np.zeros(len(doubles_select + singles_select), requires_grad=True)

Expand All @@ -239,7 +239,7 @@ def circuit_2(params, excitations, gates_select, params_select):
# PennyLane function :func:`~.pennylane.utils.sparse_hamiltonian`. This function
# returns the matrix in the SciPy `sparse coordinate <https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html>`_ format.

H_sparse = qml.utils.sparse_hamiltonian(H)
H_sparse = H.sparse_matrix()
H_sparse

##############################################################################
Expand All @@ -264,7 +264,7 @@ def circuit_2(params, excitations, gates_select, params_select):

params = np.zeros(len(excitations), requires_grad=True)

@qml.qnode(dev, diff_method="parameter-shift")
@qml.qnode(dev, diff_method="parameter-shift", interface="autograd")
def circuit(params):
qml.BasisState(hf_state, wires=range(qubits))

Expand Down
14 changes: 7 additions & 7 deletions demonstrations/tutorial_backprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
# create a device to execute the circuit on
dev = qml.device("default.qubit", wires=3)

@qml.qnode(dev, diff_method="parameter-shift")
@qml.qnode(dev, diff_method="parameter-shift", interface="autograd")
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
Expand Down Expand Up @@ -167,7 +167,7 @@ def parameter_shift(qnode, params):

dev = qml.device("default.qubit", wires=4)

@qml.qnode(dev, diff_method="parameter-shift")
@qml.qnode(dev, diff_method="parameter-shift", interface="autograd")
def circuit(params):
qml.StronglyEntanglingLayers(params, wires=[0, 1, 2, 3])
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2) @ qml.PauliZ(3))
Expand Down Expand Up @@ -264,7 +264,7 @@ def circuit(params):
# mode* for the ``default.qubit`` device.


@qml.qnode(dev, diff_method="backprop")
@qml.qnode(dev, diff_method="backprop", interface="autograd")
def circuit(params):
qml.StronglyEntanglingLayers(params, wires=[0, 1, 2, 3])
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2) @ qml.PauliZ(3))
Expand Down Expand Up @@ -336,8 +336,8 @@ def circuit(params):
# forward pass timing
# ===================

qnode_shift = qml.QNode(circuit, dev, diff_method="parameter-shift")
qnode_backprop = qml.QNode(circuit, dev, diff_method="backprop")
qnode_shift = qml.QNode(circuit, dev, diff_method="parameter-shift", interface="autograd")
qnode_backprop = qml.QNode(circuit, dev, diff_method="backprop", interface="autograd")

# parameter-shift
t = timeit.repeat("qnode_shift(params)", globals=globals(), number=num, repeat=reps)
Expand All @@ -353,8 +353,8 @@ def circuit(params):
# Gradient timing
# ===============

qnode_shift = qml.QNode(circuit, dev, diff_method="parameter-shift")
qnode_backprop = qml.QNode(circuit, dev, diff_method="backprop")
qnode_shift = qml.QNode(circuit, dev, diff_method="parameter-shift", interface="autograd")
qnode_backprop = qml.QNode(circuit, dev, diff_method="backprop", interface="autograd")

# parameter-shift
t = timeit.repeat("qml.grad(qnode_shift)(params)", globals=globals(), number=num, repeat=reps)
Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_barren_plateaus.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def rand_circuit(params, random_gate_sequence=None, num_qubits=None):

for i in range(num_samples):
gate_sequence = {i: np.random.choice(gate_set) for i in range(num_qubits)}
qcircuit = qml.QNode(rand_circuit, dev)
qcircuit = qml.QNode(rand_circuit, dev, interface="autograd")
grad = qml.grad(qcircuit, argnum=0)
params = np.random.uniform(0, 2 * np.pi, size=num_qubits)
gradient = grad(params, random_gate_sequence=gate_sequence, num_qubits=num_qubits)
Expand Down Expand Up @@ -161,7 +161,7 @@ def rand_circuit(params, random_gate_sequence=None, num_qubits=None):
grad_vals = []
for i in range(num_samples):
dev = qml.device("default.qubit", wires=num_qubits)
qcircuit = qml.QNode(rand_circuit, dev)
qcircuit = qml.QNode(rand_circuit, dev, interface="autograd")
grad = qml.grad(qcircuit, argnum=0)

gate_set = [qml.RX, qml.RY, qml.RZ]
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_classical_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def ansatz(x1, x2, thetas, amplitudes, wires):
###############################################################################
# Next, we construct the quantum node:

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def QK_circuit(x1, x2, thetas, amplitudes):
ansatz(x1, x2, thetas, amplitudes, wires = range(n_wires))
return qml.probs(wires = range(n_wires))
Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_classically_boosted_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def circuit_VQE(theta, wires):
#

dev = qml.device('default.qubit', wires=qubits)
@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def cost_fn(theta):
circuit_VQE(theta,range(qubits))
return qml.expval(H)
Expand Down Expand Up @@ -424,7 +424,7 @@ def cost_fn(theta):
wires = range(qubits + 1)
dev = qml.device("default.qubit", wires=wires)

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def hadamard_test(Uq, Ucl, component='real'):

if component == 'imag':
Expand Down
6 changes: 3 additions & 3 deletions demonstrations/tutorial_coherent_vqls.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def full_circuit(weights):

dev = qml.device("default.qubit", wires=tot_qubits)

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def global_ground(weights):
# Circuit gates
full_circuit(weights)
Expand All @@ -370,7 +370,7 @@ def global_ground(weights):
P[0, 0] = 1.0
return qml.expval(qml.Hermitian(P, wires=range(tot_qubits)))

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def ancilla_ground(weights):
# Circuit gates
full_circuit(weights)
Expand Down Expand Up @@ -489,7 +489,7 @@ def cost(weights):

dev_x = qml.device("default.qubit", wires=n_qubits, shots=n_shots)

@qml.qnode(dev_x)
@qml.qnode(dev_x, interface="autograd")
def prepare_and_sample(weights):

# Variational circuit generating a guess for the solution vector |x>
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_data_reuploading_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def density_matrix(state):
# Install any pennylane-plugin to run on some particular backend


@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def qcircuit(params, x, y):
"""A variational quantum circuit representing the Universal classifier.

Expand Down
22 changes: 11 additions & 11 deletions demonstrations/tutorial_diffable_shadows.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
H = qml.Hamiltonian([1., 1.], [qml.PauliZ(0) @ qml.PauliZ(1), qml.PauliX(0) @ qml.PauliX(1)])

dev = qml.device("default.qubit", wires=range(2), shots=10000)
@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def qnode(x, H):
qml.Hadamard(0)
qml.CNOT((0,1))
Expand All @@ -132,7 +132,7 @@ def qnode(x, H):
# class methods.

dev = qml.device("default.qubit", wires=range(2), shots=1000)
@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def qnode(x):
qml.Hadamard(0)
qml.CNOT((0,1))
Expand Down Expand Up @@ -196,7 +196,7 @@ def circuit():
obs = qml.PauliX(0) @ qml.PauliZ(3) @ qml.PauliX(6) @ qml.PauliZ(7)

dev_ideal = qml.device("default.qubit", wires=range(n_wires), shots=None)
@qml.qnode(dev_ideal)
@qml.qnode(dev_ideal, interface="autograd")
def qnode_ideal():
circuit()
return qml.expval(obs)
Expand All @@ -214,12 +214,12 @@ def qnode_ideal():
# repeating experiment 10 times to obtain averages and standard deviations
dev = qml.device("default.qubit", wires=range(10), shots=shots)

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def qnode_finite():
circuit()
return qml.expval(obs)

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def qnode_shadow():
circuit()
return qml.shadow_expval(obs)
Expand Down Expand Up @@ -297,7 +297,7 @@ def circuit():
for i in range(n):
qml.CNOT((i, (i+1)%n))

@qml.qnode(dev_ideal)
@qml.qnode(dev_ideal, interface="autograd")
def qnode_ideal():
circuit()
return qml.expval(H)
Expand All @@ -311,7 +311,7 @@ def qnode_ideal():
coeffs = np.random.rand(len(all_observables))
H = qml.Hamiltonian(coeffs, all_observables, grouping_type="qwc")

@qml.qnode(dev_ideal)
@qml.qnode(dev_ideal, interface="autograd")
def qnode_ideal():
circuit()
return qml.expval(H)
Expand All @@ -321,13 +321,13 @@ def qnode_ideal():
for _ in range(10):
dev = qml.device("default.qubit", wires=range(5), shots=shots)

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def qnode_finite():
circuit()
return qml.expval(H)

dev = qml.device("default.qubit", wires=range(5), shots=shots*n_groups)
@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def qnode_shadow():
circuit()
return qml.shadow_expval(H)
Expand Down Expand Up @@ -421,7 +421,7 @@ def circuit():

# execute qwc measurements
dev_finite = qml.device("default.qubit", wires=range(n_wires), shots=int(shots))
@qml.qnode(dev_finite)
@qml.qnode(dev_finite, interface="autograd")
def qnode_finite(H):
circuit()
return qml.expval(H)
Expand All @@ -431,7 +431,7 @@ def qnode_finite(H):

# execute shadows measurements
dev_shadow = qml.device("default.qubit", wires=range(n_wires), shots=int(shots)*n_groups)
@qml.qnode(dev_shadow)
@qml.qnode(dev_shadow, interface="autograd")
def qnode():
circuit()
return classical_shadow(wires=range(n_wires))
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_differentiable_HF.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@

dev = qml.device("default.qubit", wires=4)
def energy(mol):
@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def circuit(*args):
qml.BasisState(np.array([1, 1, 0, 0]), wires=range(4))
qml.DoubleExcitation(*args[0][0], wires=[0, 1, 2, 3])
Expand Down
6 changes: 3 additions & 3 deletions demonstrations/tutorial_doubly_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ def circuit(params):
# Now, we create three QNodes, each corresponding to a device above,
# and optimize them using gradient descent via the parameter-shift rule.

qnode_analytic = qml.QNode(circuit, dev_analytic)
qnode_stochastic = qml.QNode(circuit, dev_stochastic)
qnode_analytic = qml.QNode(circuit, dev_analytic, interface="autograd")
qnode_stochastic = qml.QNode(circuit, dev_stochastic, interface="autograd")

param_shape = StronglyEntanglingLayers.shape(n_layers=num_layers, n_wires=num_wires)
init_params = np.random.uniform(low=0, high=2*np.pi, size=param_shape, requires_grad=True)
Expand Down Expand Up @@ -293,7 +293,7 @@ def circuit(params):
)


@qml.qnode(dev_stochastic)
@qml.qnode(dev_stochastic, interface="autograd")
def circuit(params, n=None):
StronglyEntanglingLayers(weights=params, wires=[0, 1])
idx = np.random.choice(np.arange(5), size=n, replace=False)
Expand Down
8 changes: 4 additions & 4 deletions demonstrations/tutorial_expressivity_fourier_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def W(theta):
qml.Rot(theta[0], theta[1], theta[2], wires=0)


@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def serial_quantum_model(weights, x):

for theta in weights[:-1]:
Expand Down Expand Up @@ -489,7 +489,7 @@ def cost(weights, x, y):

dev = qml.device('default.qubit', wires=4)

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def ansatz(weights):
StronglyEntanglingLayers(weights, wires=range(n_qubits))
return qml.expval(qml.Identity(wires=0))
Expand Down Expand Up @@ -517,7 +517,7 @@ def W(theta):
StronglyEntanglingLayers(theta, wires=range(r))


@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def parallel_quantum_model(weights, x):

W(weights[0])
Expand Down Expand Up @@ -675,7 +675,7 @@ def W(theta):
BasicEntanglerLayers(theta, wires=range(n_qubits))


@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def quantum_model(weights, x):

W(weights[0])
Expand Down
8 changes: 4 additions & 4 deletions demonstrations/tutorial_falqon.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def expval_circuit(beta, measurement_h):
def max_clique_falqon(graph, n, beta_1, delta_t, dev):
comm_h = build_hamiltonian(graph) # Builds the commutator
cost_h, driver_h = qaoa.max_clique(graph, constrained=False) # Builds H_c and H_d
cost_fn = qml.QNode(expval_circuit, dev) # The ansatz + measurement circuit is executable
cost_fn = qml.QNode(expval_circuit, dev, interface="autograd") # The ansatz + measurement circuit is executable

beta = [beta_1] # Records each value of beta_k
energies = [] # Records the value of the cost function at each step
Expand Down Expand Up @@ -296,7 +296,7 @@ def max_clique_falqon(graph, n, beta_1, delta_t, dev):
# we can create a graph showing the probability of measuring each possible bit string.
# We define the following circuit, feeding in the optimal values of :math:`\beta_k`:

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def prob_circuit():
ansatz = build_maxclique_ansatz(cost_h, driver_h, delta_t)
ansatz(res_beta)
Expand Down Expand Up @@ -418,7 +418,7 @@ def qaoa_circuit(params, **kwargs):
qml.layer(qaoa_layer, depth, params[0], params[1])


@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def qaoa_expval(params):
qaoa_circuit(params)
return qml.expval(cost_h)
Expand Down Expand Up @@ -450,7 +450,7 @@ def qaoa_expval(params):
# define a circuit which outputs the probabilities of measuring each bit string, and
# create a bar graph:

@qml.qnode(dev)
@qml.qnode(dev, interface="autograd")
def prob_circuit(params):
qaoa_circuit(params)
return qml.probs(wires=dev.wires)
Expand Down
Loading