Skip to content

Commit

Permalink
Update templates access and remove template decorator (#360)
Browse files Browse the repository at this point in the history
* Update pyscf version. (#273)

Co-authored-by: Olivia Di Matteo <dimatteo.olivia@gmail.com>

* Update qnode.draw to qml.draw() (#352)

* pin pyparsing (#357)

* replaced pad --> pad_with in this tutorial as it has now been deprecated (#359)

* Update templates.

Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
Co-authored-by: Olivia Di Matteo <dimatteo.olivia@gmail.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
  • Loading branch information
6 people authored Nov 1, 2021
1 parent efe439d commit 6a67239
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion demonstrations/qonn.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def layer(theta, phi, wires):
M = len(wires)
phi_nonlinear = np.pi / 2

qml.templates.Interferometer(
qml.Interferometer(
theta, phi, np.zeros(M), wires=wires, mesh="triangular",
)

Expand Down
6 changes: 3 additions & 3 deletions demonstrations/quantum_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@
def permute_qubits(num_qubits):
# A random permutation
perm_order = list(rng.permutation(num_qubits))
qml.templates.Permute(perm_order, wires=list(range(num_qubits)))
qml.Permute(perm_order, wires=list(range(num_qubits)))


##############################################################################
Expand Down Expand Up @@ -383,7 +383,7 @@ def qv_circuit_layer(num_qubits):
m = 3 # number of qubits

with qml.tape.QuantumTape() as tape:
qml.templates.layer(qv_circuit_layer, m, num_qubits=m)
qml.layer(qv_circuit_layer, m, num_qubits=m)

print(tape.expand().draw(wire_order=dev_ideal.wires, show_all_wires=True))

Expand Down Expand Up @@ -628,7 +628,7 @@ def heavy_output_set(m, probs):

# Simulate the circuit analytically
with qml.tape.QuantumTape() as tape:
qml.templates.layer(qv_circuit_layer, m, num_qubits=m)
qml.layer(qv_circuit_layer, m, num_qubits=m)
qml.probs(wires=range(m))

output_probs = tape.expand().execute(dev_ideal).reshape(2 ** m, )
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/spsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@


def circuit(params):
qml.templates.StronglyEntanglingLayers(params, wires=list(range(num_wires)))
qml.StronglyEntanglingLayers(params, wires=list(range(num_wires)))
return qml.expval(all_pauliz_tensor_prod)


Expand Down
6 changes: 3 additions & 3 deletions demonstrations/tutorial_backprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def parameter_shift(qnode, params):

@qml.qnode(dev, diff_method="parameter-shift", mutable=False)
def circuit(params):
qml.templates.StronglyEntanglingLayers(params, wires=[0, 1, 2, 3])
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 @@ -271,7 +271,7 @@ def circuit(params):

@qml.qnode(dev, diff_method="backprop")
def circuit(params):
qml.templates.StronglyEntanglingLayers(params, wires=[0, 1, 2, 3])
qml.StronglyEntanglingLayers(params, wires=[0, 1, 2, 3])
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2) @ qml.PauliZ(3))

# initialize circuit parameters
Expand Down Expand Up @@ -315,7 +315,7 @@ def circuit(params):
dev = qml.device("default.qubit", wires=4)

def circuit(params):
qml.templates.StronglyEntanglingLayers(params, wires=[0, 1, 2, 3])
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
4 changes: 2 additions & 2 deletions demonstrations/tutorial_falqon.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ def build_hamiltonian(graph):
# the form :math:`U_d(\beta_k) U_c`. Note that we can use the :class:`~.pennylane.templates.ApproxTimeEvolution` template:

def falqon_layer(beta_k, cost_h, driver_h, delta_t):
qml.templates.ApproxTimeEvolution(cost_h, delta_t, 1)
qml.templates.ApproxTimeEvolution(driver_h, delta_t * beta_k, 1)
qml.ApproxTimeEvolution(cost_h, delta_t, 1)
qml.ApproxTimeEvolution(driver_h, delta_t * beta_k, 1)

######################################################################
# We then define a method which returns a FALQON ansatz corresponding to a particular cost Hamiltonian, driver
Expand Down
16 changes: 8 additions & 8 deletions demonstrations/tutorial_measurement_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
singles, doubles = qml.qchem.excitations(electrons, num_qubits)
s_wires, d_wires = qml.qchem.excitations_to_wires(singles, doubles)
ansatz = functools.partial(
qml.templates.UCCSD, init_state=initial_state, s_wires=s_wires, d_wires=d_wires
qml.UCCSD, init_state=initial_state, s_wires=s_wires, d_wires=d_wires
)

# generate the cost function
Expand Down Expand Up @@ -388,13 +388,13 @@ def cost_circuit(params):

@qml.qnode(dev)
def circuit1(weights):
qml.templates.StronglyEntanglingLayers(weights, wires=range(3))
qml.StronglyEntanglingLayers(weights, wires=range(3))
return qml.expval(obs[0])


@qml.qnode(dev)
def circuit2(weights):
qml.templates.StronglyEntanglingLayers(weights, wires=range(3))
qml.StronglyEntanglingLayers(weights, wires=range(3))
return qml.expval(obs[1])


Expand All @@ -409,7 +409,7 @@ def circuit2(weights):

@qml.qnode(dev)
def circuit_qwc(weights):
qml.templates.StronglyEntanglingLayers(weights, wires=range(3))
qml.StronglyEntanglingLayers(weights, wires=range(3))

# rotate wire 0 into the shared eigenbasis
qml.RY(-np.pi / 2, wires=0)
Expand Down Expand Up @@ -455,7 +455,7 @@ def circuit_qwc(weights):

@qml.qnode(dev)
def circuit(weights):
qml.templates.StronglyEntanglingLayers(weights, wires=range(3))
qml.StronglyEntanglingLayers(weights, wires=range(3))
return [
qml.expval(qml.PauliX(0) @ qml.PauliY(1)),
qml.expval(qml.PauliX(0) @ qml.PauliZ(2))
Expand Down Expand Up @@ -489,7 +489,7 @@ def circuit(weights):
#
# @qml.qnode(dev)
# def circuit(weights):
# qml.templates.StronglyEntanglingLayers(weights, wires=range(3))
# qml.StronglyEntanglingLayers(weights, wires=range(3))
# return [
# qml.expval(qml.PauliZ(0) @ qml.PauliY(1)),
# qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
Expand Down Expand Up @@ -732,7 +732,7 @@ def format_pauli_word(term):

@qml.qnode(dev)
def circuit(weights, group=None, **kwargs):
qml.templates.StronglyEntanglingLayers(weights, wires=range(4))
qml.StronglyEntanglingLayers(weights, wires=range(4))
return [qml.expval(o) for o in group]

weights = qml.init.strong_ent_layers_normal(n_layers=3, n_wires=4)
Expand All @@ -755,7 +755,7 @@ def circuit(weights, group=None, **kwargs):
# optimized:

H = qml.Hamiltonian(coeffs=np.ones(len(terms)), observables=terms)
cost_fn = qml.ExpvalCost(qml.templates.StronglyEntanglingLayers, H, dev, optimize=True)
cost_fn = qml.ExpvalCost(qml.StronglyEntanglingLayers, H, dev, optimize=True)
print(cost_fn(weights))

##############################################################################
Expand Down
4 changes: 3 additions & 1 deletion demonstrations/tutorial_multiclass_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def layer(W):


def circuit(weights, feat=None):
qml.templates.embeddings.AmplitudeEmbedding(feat, range(num_qubits), pad_with=0.0, normalize=True)

qml.AmplitudeEmbedding(feat, range(num_qubits), pad=0.0, normalize=True)

for W in weights:
layer(W)

Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_qaoa_intro.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@

@qml.qnode(dev)
def circuit():
qml.templates.ApproxTimeEvolution(H, t, n)
qml.ApproxTimeEvolution(H, t, n)
return [qml.expval(qml.PauliZ(i)) for i in range(2)]

print(qml.draw(circuit)())
Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_qnn_module_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@

@qml.qnode(dev)
def qnode(inputs, weights):
qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.templates.BasicEntanglerLayers(weights, wires=range(n_qubits))
qml.AngleEmbedding(inputs, wires=range(n_qubits))
qml.BasicEntanglerLayers(weights, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]

###############################################################################
Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_qnn_module_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@

@qml.qnode(dev)
def qnode(inputs, weights):
qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.templates.BasicEntanglerLayers(weights, wires=range(n_qubits))
qml.AngleEmbedding(inputs, wires=range(n_qubits))
qml.BasicEntanglerLayers(weights, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]

###############################################################################
Expand Down
7 changes: 2 additions & 5 deletions demonstrations/tutorial_quantum_metrology.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@
from pennylane_cirq import ops as cirq_ops


@qml.template
def encoding(phi, gamma):
for i in range(3):
qml.RZ(phi[i], wires=[i])
Expand All @@ -156,16 +155,14 @@ def encoding(phi, gamma):
# we make use of the
# `ArbitraryStatePreparation <https://pennylane.readthedocs.io/en/stable/code/api/pennylane.templates.state_preparations.ArbitraryStatePreparation.html>`_
# template from PennyLane.
@qml.template
def ansatz(weights):
qml.templates.ArbitraryStatePreparation(weights, wires=[0, 1, 2])
qml.ArbitraryStatePreparation(weights, wires=[0, 1, 2])

NUM_ANSATZ_PARAMETERS = 14

@qml.template
def measurement(weights):
for i in range(3):
qml.templates.ArbitraryStatePreparation(
qml.ArbitraryStatePreparation(
weights[2 * i : 2 * (i + 1)], wires=[i]
)

Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_vqe_spin_sectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@


def circuit(params, wires):
qml.templates.AllSinglesDoubles(params, wires, hf, singles, doubles)
qml.AllSinglesDoubles(params, wires, hf, singles, doubles)


##############################################################################
Expand Down Expand Up @@ -278,7 +278,7 @@ def total_spin(params):


def circuit(params, wires):
qml.templates.AllSinglesDoubles(params, wires, np.flip(hf), singles, doubles)
qml.AllSinglesDoubles(params, wires, np.flip(hf), singles, doubles)


##############################################################################
Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_vqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def single_rotation(phi_params, qubits):

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


######################################################################
Expand All @@ -257,7 +257,7 @@ def single_rotation(phi_params, qubits):
def quantum_circuit(rotation_params, coupling_params, sample=None):

# Prepares the initial basis state corresponding to the sample
qml.templates.BasisStatePreparation(sample, wires=range(nr_qubits))
qml.BasisStatePreparation(sample, wires=range(nr_qubits))

# Prepares the variational ansatz for the circuit
for i in range(0, depth):
Expand Down

0 comments on commit 6a67239

Please sign in to comment.