Skip to content
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
38 changes: 19 additions & 19 deletions demonstrations_v2/tutorial_vqls/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
"""

# Pennylane
import pennylane as qml
import pennylane as qp
from pennylane import numpy as np

# Plotting
Expand Down Expand Up @@ -219,7 +219,7 @@
def U_b():
"""Unitary matrix rotating the ground state to the problem vector |b> = U_b |0>."""
for idx in range(n_qubits):
qml.Hadamard(wires=idx)
qp.Hadamard(wires=idx)

def CA(idx):
"""Controlled versions of the unitary components A_l of the problem matrix A."""
Expand All @@ -228,11 +228,11 @@ def CA(idx):
None

elif idx == 1:
qml.CNOT(wires=[ancilla_idx, 0])
qml.CZ(wires=[ancilla_idx, 1])
qp.CNOT(wires=[ancilla_idx, 0])
qp.CZ(wires=[ancilla_idx, 1])

elif idx == 2:
qml.CNOT(wires=[ancilla_idx, 0])
qp.CNOT(wires=[ancilla_idx, 0])


##############################################################################
Expand All @@ -256,11 +256,11 @@ def variational_block(weights):
"""Variational circuit mapping the ground state |0> to the ansatz state |x>."""
# We first prepare an equal superposition of all the states of the computational basis.
for idx in range(n_qubits):
qml.Hadamard(wires=idx)
qp.Hadamard(wires=idx)

# A very minimal variational circuit.
for idx, element in enumerate(weights):
qml.RY(element, wires=idx)
qp.RY(element, wires=idx)


##############################################################################
Expand All @@ -276,18 +276,18 @@ def variational_block(weights):
# and will be used to estimate the coefficients :math:`\mu_{l,l',j}` defined in the introduction.
# A graphical representation of this circuit is shown at the top of this tutorial.

dev_mu = qml.device("lightning.qubit", wires=tot_qubits)
dev_mu = qp.device("lightning.qubit", wires=tot_qubits)

@qml.qnode(dev_mu, interface="autograd")
@qp.qnode(dev_mu, interface="autograd")
def local_hadamard_test(weights, l=None, lp=None, j=None, part=None):

# First Hadamard gate applied to the ancillary qubit.
qml.Hadamard(wires=ancilla_idx)
qp.Hadamard(wires=ancilla_idx)

# For estimating the imaginary part of the coefficient "mu", we must add a "-i"
# phase gate.
if part == "Im" or part == "im":
qml.PhaseShift(-np.pi / 2, wires=ancilla_idx)
qp.PhaseShift(-np.pi / 2, wires=ancilla_idx)

# Variational circuit generating a guess for the solution vector |x>
variational_block(weights)
Expand All @@ -301,7 +301,7 @@ def local_hadamard_test(weights, l=None, lp=None, j=None, part=None):

# Controlled Z operator at position j. If j = -1, apply the identity.
if j != -1:
qml.CZ(wires=[ancilla_idx, j])
qp.CZ(wires=[ancilla_idx, j])

# Unitary U_b associated to the problem vector |b>.
U_b()
Expand All @@ -311,10 +311,10 @@ def local_hadamard_test(weights, l=None, lp=None, j=None, part=None):
CA(lp)

# Second Hadamard gate applied to the ancillary qubit.
qml.Hadamard(wires=ancilla_idx)
qp.Hadamard(wires=ancilla_idx)

# Expectation value of Z for the ancillary qubit.
return qml.expval(qml.PauliZ(wires=ancilla_idx))
return qp.expval(qp.PauliZ(wires=ancilla_idx))


##############################################################################################
Expand Down Expand Up @@ -382,7 +382,7 @@ def cost_loc(weights):

##############################################################################
# To minimize the cost function we use the gradient-descent optimizer.
opt = qml.GradientDescentOptimizer(eta)
opt = qp.GradientDescentOptimizer(eta)


##############################################################################
Expand Down Expand Up @@ -463,10 +463,10 @@ def cost_loc(weights):
# For this task, we initialize a new PennyLane device and define the associated
# *qnode* circuit.

dev_x = qml.device("lightning.qubit", wires=n_qubits)
dev_x = qp.device("lightning.qubit", wires=n_qubits)

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

# Variational circuit generating a guess for the solution vector |x>
Expand All @@ -475,7 +475,7 @@ def prepare_and_sample(weights):
# We assume that the system is measured in the computational basis.
# then sampling the device will give us a value of 0 or 1 for each qubit (n_qubits)
# this will be repeated for the total number of shots provided (n_shots)
return qml.sample()
return qp.sample()


##############################################################################
Expand Down
2 changes: 1 addition & 1 deletion demonstrations_v2/tutorial_vqls/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"executable_stable": true,
"executable_latest": true,
"dateOfPublication": "2019-11-04T00:00:00+00:00",
"dateOfLastModification": "2025-10-15T00:00:00+00:00",
"dateOfLastModification": "2026-04-17T00:00:00+00:00",
"categories": [
"Optimization"
],
Expand Down
24 changes: 12 additions & 12 deletions demonstrations_v2/tutorial_vqt/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
#


import pennylane as qml
import pennylane as qp
from matplotlib import pyplot as plt
import numpy as np
import scipy
Expand Down Expand Up @@ -148,9 +148,9 @@ def create_hamiltonian_matrix(n, graph):
x = y = z = 1
for j in range(0, n):
if j == i[0] or j == i[1]:
x = np.kron(x, qml.matrix(qml.PauliX(0)))
y = np.kron(y, qml.matrix(qml.PauliY(0)))
z = np.kron(z, qml.matrix(qml.PauliZ(0)))
x = np.kron(x, qp.matrix(qp.PauliX(0)))
y = np.kron(y, qp.matrix(qp.PauliY(0)))
z = np.kron(z, qp.matrix(qp.PauliZ(0)))
else:
x = np.kron(x, np.identity(2))
y = np.kron(y, np.identity(2))
Expand Down Expand Up @@ -237,7 +237,7 @@ def single_rotation(phi_params, qubits):

rotations = ["Z", "Y", "X"]
for i in range(0, len(rotations)):
qml.AngleEmbedding(phi_params[i], wires=qubits, rotation=rotations[i])
qp.AngleEmbedding(phi_params[i], wires=qubits, rotation=rotations[i])


######################################################################
Expand All @@ -254,32 +254,32 @@ def CRX_ring(parameters, wires):
n_wires = len(wires)

for param, w in zip(parameters, wires):
qml.CRX(param, wires=[w % n_wires, (w + 1) % n_wires])
qp.CRX(param, wires=[w % n_wires, (w + 1) % n_wires])


depth = 4
dev = qml.device("lightning.qubit", wires=nr_qubits)
dev = qp.device("lightning.qubit", wires=nr_qubits)


def quantum_circuit(rotation_params, coupling_params, sample=None, return_state=False):

# Prepares the initial basis state corresponding to the sample
qml.BasisState(sample, wires=range(nr_qubits))
qp.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))
CRX_ring(coupling_params[i], list(range(nr_qubits)))

if return_state:
return qml.state()
return qp.state()

# Calculates the expectation value of the Hamiltonian with respect to the prepared states
return qml.expval(qml.Hermitian(ham_matrix, wires=range(nr_qubits)))
return qp.expval(qp.Hermitian(ham_matrix, wires=range(nr_qubits)))


# Constructs the QNode
qnode = qml.QNode(quantum_circuit, dev, interface="autograd")
qnode = qp.QNode(quantum_circuit, dev, interface="autograd")


######################################################################
Expand All @@ -291,7 +291,7 @@ def quantum_circuit(rotation_params, coupling_params, sample=None, return_state=
rotation_params = [[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] for i in range(0, depth)]
coupling_params = [[1, 1, 1, 1] for i in range(0, depth)]
print(
qml.draw(qnode, level="device", show_matrices=True)(
qp.draw(qnode, level="device", show_matrices=True)(
rotation_params, coupling_params, sample=[1, 0, 1, 0]
)
)
Expand Down
2 changes: 1 addition & 1 deletion demonstrations_v2/tutorial_vqt/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"executable_stable": true,
"executable_latest": true,
"dateOfPublication": "2020-07-07T00:00:00+00:00",
"dateOfLastModification": "2025-09-22T15:48:14+00:00",
"dateOfLastModification": "2026-04-17T15:48:14+00:00",
"categories": [
"Optimization"
],
Expand Down
Loading
Loading