Skip to content

Commit

Permalink
Remove use of the init module everywhere (#361)
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)

* Add an action for demo output checking (#356)

* Update demos to make use of the new qml.sample and qml.expval(H) functionality (story #: 8118)  (#330)

* Updated qsim to use the new qml.sample functionality
**needs to be tested ! **

* just removed one commented line

* Updated one of the paragraphs to be consistent with
the changes

* Updated coherent vqls to use the builtin functionality of
qml.sample, also tested it locally and it seems to run without
errors!

* updated qaoa_maxcut to use new qml.sample functionality

* updated vqls tutorial to make  use of the new functionality
of qml.sample

* Made some changes to chem reaction tutorial, want to confirm with
CI to see if it works properly because I can't run it locally

* fixed quick little bug with wires in circuit def

* quick change to help debug error in CI

* Found the bug with chem_reactions tutorial, hopefully
this fixes it

* updated falqon tutorial and removed dependance on qml.ExpvalCost,
still need to confirm it works with CI

* fixed a small bug, wrote Qnode isntead of QNode

* Updated falqon to use qml.expval instead of qml.ExpvalCost

* updated optimized measurement tutorial to make use of qml.expval,
still need to test with CI

* debugging measurement optimize tutorial

* The bug regarding Qnodes is well known, just made a change
the Christina suggested as a temporary fix

* Updated molecular geometry optimization tutorial to make
use of qml.expval

* Updated qaoa tutorial to make use of qml.expval, updated docs
in both examples to remove qml.ExpValCost

* Updated vqe and vqe qng tutorials to use qml.expval(H)

* Updated the vqe soin sectors demo with qml.expval, fixed a typo in
overview of vqe tutorial

* Updated parallel vqe tutorial to remove dependance on qml.ExpvalCost,
was not able to fully test this because I don't have rigitti setup

* Updated these tutorials to remove qml.ExpvalCost dependance

* Cleaning up some typos and comments

* Updated measurement optimization tutorial to make use of qml.Tracker

* Update demonstrations/tutorial_qaoa_maxcut.py

Co-authored-by: antalszava <antalszava@gmail.com>

* Update demonstrations/qsim_beyond_classical.py

Co-authored-by: antalszava <antalszava@gmail.com>

* Update demonstrations/tutorial_coherent_vqls.py

Co-authored-by: antalszava <antalszava@gmail.com>

* Updated tutorials based on pr comments

* Fixed bug in the spsa demo

* clean up

* Update demonstrations/tutorial_qaoa_maxcut.py

Co-authored-by: Maria Schuld <mariaschuld@gmail.com>

* cleaned up docs

* Cleaning up documentation

* added comment

* Update demonstrations/braket-parallel-gradients.py

Co-authored-by: antalszava <antalszava@gmail.com>

* Clean up

- added variable num_wires to mol_geo_opt tutorial

- replaced new_graph.nodes with dev.wires to clairfy
iteration over wires

* Update demonstrations/tutorial_coherent_vqls.py

Co-authored-by: Maria Schuld <mariaschuld@gmail.com>

* Update demonstrations/qsim_beyond_classical.py

Co-authored-by: Maria Schuld <mariaschuld@gmail.com>

* Update demonstrations/tutorial_coherent_vqls.py

Co-authored-by: Maria Schuld <mariaschuld@gmail.com>

* Update demonstrations/tutorial_mol_geo_opt.py

Co-authored-by: Maria Schuld <mariaschuld@gmail.com>

* Update demonstrations/tutorial_qaoa_intro.py

Co-authored-by: Maria Schuld <mariaschuld@gmail.com>

* Update demonstrations/tutorial_vqe_qng.py

Co-authored-by: Maria Schuld <mariaschuld@gmail.com>

* Update demonstrations/tutorial_vqls.py

Co-authored-by: Maria Schuld <mariaschuld@gmail.com>

* Update demonstrations/tutorial_mol_geo_opt.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update demonstrations/tutorial_qaoa_maxcut.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Address comments

- made circuit a qnode to prevent the need to initialize multiple
qnodes one after another

Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: Maria Schuld <mariaschuld@gmail.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>

* sources + yml

* add diff job

* on push

* branch name

* unzip with path

* updates

* tutorials from master and dev too

* path to script

* Update diffs found

* add toc and references

* Update diffs found

* comments

* mv to .github/workflows dedicated folder

* diffs document updates

* revert changes to tutorial_vqe_qng.py

* Update .github/workflows/html_parser.py

Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>

* suggestions

* no sys

* Move demo_diff.md so that workflows access is not required

* change trigger and branch

Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
Co-authored-by: Maria Schuld <mariaschuld@gmail.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: QML demo checker Bot <>

* remove init module everywhere

* minor improvement

* add 2pi scaling

* Update demonstrations/spsa.py

Co-authored-by: antalszava <antalszava@gmail.com>

* Update demonstrations/tutorial_doubly_stochastic.py

Co-authored-by: antalszava <antalszava@gmail.com>

* add low and high; scale argument

* add low and high; scale argument

* use pad_with

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 6a67239 commit e18a809
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
8 changes: 4 additions & 4 deletions demonstrations/spsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
the pieces of an example optimization to come together:
* a circuit ansatz: :func:`~.pennylane.templates.layers.StronglyEntanglingLayers`,
* initial parameters: conveniently generated using :func:`~.pennylane.init.strong_ent_layers_normal`,
* initial parameters: the correct shape can be computed by :func:`~.pennylane.templates.layers.StronglyEntanglingLayers.shape`,
* an observable: :math:`\bigotimes_{i=0}^{N-1}\sigma_z^i`, where :math:`N` stands
for the number of qubits.
Expand Down Expand Up @@ -191,9 +191,9 @@ def circuit(params):
# be a flattened array. As a result, our cost function must accept a flat array of parameters
# to be optimized.
flat_shape = num_layers * num_wires * 3
init_params = qml.init.strong_ent_layers_normal(
n_wires=num_wires, n_layers=num_layers
)
param_shape = qml.templates.StronglyEntanglingLayers.shape(n_wires=num_wires, n_layers=num_layers)
init_params = np.random.normal(scale=0.1, size=param_shape)

init_params_spsa = init_params.reshape(flat_shape)

qnode_spsa = qml.QNode(circuit, dev_sampler_spsa)
Expand Down
9 changes: 6 additions & 3 deletions demonstrations/tutorial_backprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def circuit(params):


# initialize circuit parameters
params = qml.init.strong_ent_layers_normal(n_wires=4, n_layers=15)
param_shape = qml.templates.StronglyEntanglingLayers.shape(n_wires=4, n_layers=15)
params = np.random.normal(scale=0.1, size=param_shape)
print(params.size)
print(circuit(params))

Expand Down Expand Up @@ -275,7 +276,8 @@ def circuit(params):
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2) @ qml.PauliZ(3))

# initialize circuit parameters
params = qml.init.strong_ent_layers_normal(n_wires=4, n_layers=15)
param_shape = qml.templates.StronglyEntanglingLayers.shape(n_wires=4, n_layers=15)
params = np.random.normal(scale=0.1, size=param_shape)
params = np.array(params, requires_grad=True)
print(circuit(params))

Expand Down Expand Up @@ -334,7 +336,8 @@ def circuit(params):
gradient_backprop = []

for depth in range(0, 21):
params = qml.init.strong_ent_layers_normal(n_wires=4, n_layers=depth)
param_shape = qml.templates.StronglyEntanglingLayers.shape(n_wires=4, n_layers=depth)
params = np.random.normal(scale=0.1, size=param_shape)
num_params = params.size
params = np.array(params, requires_grad=True)

Expand Down
4 changes: 2 additions & 2 deletions demonstrations/tutorial_doubly_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
np.random.seed(3)

from pennylane import expval
from pennylane.init import strong_ent_layers_uniform
from pennylane.templates.layers import StronglyEntanglingLayers

num_layers = 2
Expand Down Expand Up @@ -155,7 +154,8 @@ def circuit(params):
qnode_analytic = qml.QNode(circuit, dev_analytic)
qnode_stochastic = qml.QNode(circuit, dev_stochastic)

init_params = strong_ent_layers_uniform(num_layers, num_wires)
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)

# Optimizing using exact gradient descent

Expand Down
7 changes: 4 additions & 3 deletions demonstrations/tutorial_measurement_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ def circuit2(weights):
qml.StronglyEntanglingLayers(weights, wires=range(3))
return qml.expval(obs[1])


weights = qml.init.strong_ent_layers_normal(n_layers=3, n_wires=3)
param_shape = qml.templates.StronglyEntanglingLayers.shape(n_layers=3, n_wires=3)
weights = np.random.normal(scale=0.1, size=param_shape)

print("Expectation value of XYI = ", circuit1(weights))
print("Expectation value of XIZ = ", circuit2(weights))
Expand Down Expand Up @@ -735,7 +735,8 @@ def circuit(weights, group=None, **kwargs):
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)
param_shape = qml.templates.StronglyEntanglingLayers.shape(n_layers=3, n_wires=4)
weights = np.random.normal(scale=0.1, size=param_shape)
result = [circuit(weights, group=g) for g in obs_groupings]

print("Term expectation values:")
Expand Down
2 changes: 1 addition & 1 deletion demonstrations/tutorial_multiclass_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def layer(W):

def circuit(weights, feat=None):

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

for W in weights:
layer(W)
Expand Down
5 changes: 2 additions & 3 deletions demonstrations/tutorial_rosalin.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@
# and begin constructing some QNodes to evaluate each observable. For our ansatz, we'll use the
# :class:`~.pennylane.templates.layers.StronglyEntanglingLayers`.

from pennylane import expval
from pennylane.init import strong_ent_layers_uniform
from pennylane.templates.layers import StronglyEntanglingLayers

num_layers = 2
Expand Down Expand Up @@ -214,7 +212,8 @@ def cost(params):
# Evaluating our cost function with some initial parameters, we can test out
# that our cost function evaluates correctly.

init_params = strong_ent_layers_uniform(n_layers=num_layers, n_wires=num_wires)
param_shape = StronglyEntanglingLayers.shape(n_layers=num_layers, n_wires=num_wires)
init_params = np.random.uniform(low=0.0, high=2*np.pi, size=param_shape)
print(cost(init_params))


Expand Down

0 comments on commit e18a809

Please sign in to comment.