We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Missing plot lines in VQE tutorial 
#### Issue description

The second and third plots in the VQE tutorial do not show the plot line of the gradient descent. The legends are shown, however. 

Is this problem coming from making this file a "run" file? 

https://pennylane.readthedocs.io/en/latest/tutorials/pennylane_run_variational_quantum_eigensolver.html#vqe

* *Expected behavior:*

The plots should show the gradient descent on top of the optimization landscape. 



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `examples/pennylane_run_variational_quantum_eigensolver.py`
Content:
```
1 r"""
2 .. _vqe:
3 
4 Variational quantum eigensolver
5 ===============================
6 
7 This example demonstrates the principle of a variational quantum
8 eigensolver (VQE), originally proposed in `Peruzzo et al.
9 (2014) <https://www.nature.com/articles/ncomms5213>`__. To showcase the
10 hybrid computational capabilities of PennyLane, we first train a quantum
11 circuit to minimize the squared energy expectation for a Hamiltonian
12 :math:`H`,
13 
14 .. math::
15 
16     \langle \psi_v | H | \psi_v \rangle^2  =( 0.1 \langle \psi_{v} | X_2 |
17     \psi_v \rangle + 0.5 \langle \psi_v | Y_2 | \psi_v \rangle )^2.
18 
19 Here, :math:`|\psi_v\rangle` is the state
20 obtained after applying a quantum circuit to an initial state
21 :math:`|0\rangle`. The quantum circuit depends on trainable variables
22 :math:`v = \{v_1, v_2\}`, and :math:`X_2`, :math:`Y_2` denote the
23 Pauli-X and Pauli-Y operator acting on the second qubit (*Note: We apply
24 the square to make the optimization landscapes more interesting, but in
25 common applications the cost is directly the energy expectation value*).
26 
27 After doing this, we will then turn things around and use a fixed
28 quantum circuit to prepare a state :math:`|\psi\rangle`, but train the coefficients of
29 the Hamiltonian to minimize
30 
31 .. math::
32 
33     \langle \psi | H | \psi \rangle^2  = (v_1 \langle \psi | X_2 | \psi
34     \rangle + v_2 \langle \psi | Y_2 | \psi \rangle )^2 .
35 """
36 
37 ##############################################################################
38 # 1. Optimizing the quantum circuit
39 # ---------------------------------
40 #
41 # Imports
42 # ~~~~~~~
43 #
44 # We begin by importing PennyLane, the PennyLane-wrapped version of NumPy,
45 # and the GradientDescentOptimizer.
46 
47 import pennylane as qml
48 from pennylane import numpy as np
49 from pennylane.optimize import GradientDescentOptimizer
50 
51 ##############################################################################
52 # We use the default qubit simulator as a device.
53 
54 dev = qml.device("default.qubit", wires=2)
55 
56 ##############################################################################
57 # Quantum nodes
58 # ~~~~~~~~~~~~~
59 #
60 # The quantum circuit of the variational eigensolver is an ansatz that
61 # defines a manifold of possible quantum states. We use a Hadamard, two
62 # rotations and a CNOT gate to construct our circuit.
63 
64 
65 def ansatz(var):
66     qml.Rot(0.3, 1.8, 5.4, wires=1)
67     qml.RX(var[0], wires=0)
68     qml.RY(var[1], wires=1)
69     qml.CNOT(wires=[0, 1])
70 
71 
72 ##############################################################################
73 # A variational eigensolver requires us to evaluate expectations of
74 # different Pauli operators. In this example, the Hamiltonian is expressed
75 # by only two single-qubit Pauli operators, namely the X and Y operator
76 # applied to the first qubit.
77 #
78 # Since these operators will be measured on the same wire, we will need to
79 # create two quantum nodes (one for each operator whose expectation value
80 # we measure), but we can reuse the same device.
81 #
82 # .. note::
83 #
84 #     If the Pauli observables were evaluated on different wires, we
85 #     could use one quantum node and return a tuple of expectations in only
86 #     one quantum node:
87 #     ``return qml.expectation.PauliX(0), qml.expectation.PauliY(1)``
88 
89 
90 @qml.qnode(dev)
91 def circuit_X(var):
92     ansatz(var)
93     return qml.expval(qml.PauliX(1))
94 
95 
96 @qml.qnode(dev)
97 def circuit_Y(var):
98     ansatz(var)
99     return qml.expval(qml.PauliY(1))
100 
101 
102 ##############################################################################
103 # Objective
104 # ~~~~~~~~~
105 
106 # The cost function to be optimized in VQE is simply a linear combination
107 # of the expectations, which defines the expectation of the Hamiltonian we
108 # are interested in. In our case, we square this cost function to provide
109 # a more interesting landscape with the same minima.
110 
111 
112 def cost(var):
113     expX = circuit_X(var)
114     expY = circuit_Y(var)
115     return (0.1 * expX + 0.5 * expY) ** 2
116 
117 
118 ##############################################################################
119 # This cost defines the following landscape:
120 #
121 # *Note: To run the following cell you need the matplotlib library.*
122 
123 import matplotlib.pyplot as plt
124 from mpl_toolkits.mplot3d import Axes3D
125 from matplotlib import cm
126 from matplotlib.ticker import MaxNLocator
127 
128 fig = plt.figure(figsize=(6, 4))
129 ax = fig.gca(projection="3d")
130 
131 X = np.linspace(-3.0, 3.0, 20)
132 Y = np.linspace(-3.0, 3.0, 20)
133 xx, yy = np.meshgrid(X, Y)
134 Z = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))
135 surf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)
136 
137 ax.set_xlabel("v1")
138 ax.set_ylabel("v2")
139 ax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune="lower"))
140 
141 plt.show()
142 
143 ##############################################################################
144 # Optimization
145 # ~~~~~~~~~~~~
146 #
147 # We create a GradientDescentOptimizer and use it to optimize the cost
148 # function.
149 
150 opt = GradientDescentOptimizer(0.5)
151 
152 var = [0.3, 2.5]
153 var_gd = [var]
154 for it in range(20):
155     var = opt.step(cost, var)
156     var_gd.append(var)
157 
158     print(
159         "Cost after step {:5d}: {: .7f} | Variables: [{: .5f},{: .5f}]".format(
160             it + 1, cost(var), var[0], var[1]
161         )
162     )
163 
164 ##############################################################################
165 # We can plot the path that the variables took during gradient descent. To
166 # make the plot more clear, we will shorten the range for :math:`v_2`.
167 
168 fig = plt.figure(figsize=(6, 4))
169 ax = fig.gca(projection="3d")
170 
171 X = np.linspace(-3, np.pi / 2, 20)
172 Y = np.linspace(-3, 3, 20)
173 xx, yy = np.meshgrid(X, Y)
174 Z = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))
175 surf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)
176 
177 path_z = [cost(var) + 1e-8 for var in var_gd]
178 path_x = [v[0] for v in var_gd]
179 path_y = [v[1] for v in var_gd]
180 ax.plot(path_x, path_y, path_z, c="green", marker=".", label="graddesc")
181 
182 ax.set_xlabel("v1")
183 ax.set_ylabel("v2")
184 ax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune="lower"))
185 
186 plt.legend()
187 plt.show()
188 
189 
190 ##############################################################################
191 # 2. Optimizing the Hamiltonian coefficients
192 # ------------------------------------------
193 #
194 # Instead of optimizing the circuit parameters, we can also use a fixed
195 # circuit,
196 
197 
198 def ansatz():
199     qml.Rot(0.3, 1.8, 5.4, wires=1)
200     qml.RX(-0.5, wires=0)
201     qml.RY(0.5, wires=1)
202     qml.CNOT(wires=[0, 1])
203 
204 
205 @qml.qnode(dev)
206 def circuit_X():
207     ansatz()
208     return qml.expval(qml.PauliX(1))
209 
210 
211 @qml.qnode(dev)
212 def circuit_Y():
213     ansatz()
214     return qml.expval(qml.PauliY(1))
215 
216 
217 ##############################################################################
218 # and make the classical coefficients that appear in the Hamiltonian the
219 # trainable variables.
220 
221 
222 def cost(var):
223     expX = circuit_X()
224     expY = circuit_Y()
225     return (var[0] * expX + var[1] * expY) ** 2
226 
227 
228 opt = GradientDescentOptimizer(0.5)
229 
230 var = [0.3, 2.5]
231 var_gd = [var]
232 for it in range(20):
233     var = opt.step(cost, var)
234     var_gd.append(var)
235 
236     print(
237         "Cost after step {:5d}: {: .7f} | Variables: [{: .5f},{: .5f}]".format(
238             it + 1, cost(var), var[0], var[1]
239         )
240     )
241 
242 ##############################################################################
243 # The landscape has a quadratic shape.
244 
245 fig = plt.figure(figsize=(6, 4))
246 ax = fig.gca(projection="3d")
247 
248 X = np.linspace(-3, np.pi / 2, 20)
249 Y = np.linspace(-3, 3, 20)
250 xx, yy = np.meshgrid(X, Y)
251 Z = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))
252 surf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)
253 
254 path_z = [cost(var) + 1e-8 for var in var_gd]
255 path_x = [v[0] for v in var_gd]
256 path_y = [v[1] for v in var_gd]
257 ax.plot(path_x, path_y, path_z, c="pink", marker=".", label="graddesc")
258 
259 ax.set_xlabel("v1")
260 ax.set_ylabel("v2")
261 ax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune="lower"))
262 
263 plt.legend()
264 plt.show()
265 
266 
267 ##############################################################################
268 # 3. Optimizing classical and quantum parameters
269 # ----------------------------------------------
270 
271 # Finally, we can optimize *classical* and *quantum* weights together by
272 # combining the two approaches from above.
273 
274 
275 def ansatz(var):
276 
277     qml.Rot(0.3, 1.8, 5.4, wires=1)
278     qml.RX(var[0], wires=0)
279     qml.RY(var[1], wires=1)
280     qml.CNOT(wires=[0, 1])
281 
282 
283 @qml.qnode(dev)
284 def circuit_X(var):
285     ansatz(var)
286     return qml.expval(qml.PauliX(1))
287 
288 
289 @qml.qnode(dev)
290 def circuit_Y(var):
291     ansatz(var)
292     return qml.expval(qml.PauliY(1))
293 
294 
295 def cost(var):
296 
297     expX = circuit_X(var)
298     expY = circuit_Y(var)
299 
300     return (var[2] * expX + var[3] * expY) ** 2
301 
302 
303 opt = GradientDescentOptimizer(0.5)
304 var = [0.3, 2.5, 0.3, 2.5]
305 
306 for it in range(10):
307     var = opt.step(cost, var)
308     print("Cost after step {:5d}: {: 0.7f}".format(it + 1, cost(var)))
309
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/examples/pennylane_run_variational_quantum_eigensolver.py b/examples/pennylane_run_variational_quantum_eigensolver.py
--- a/examples/pennylane_run_variational_quantum_eigensolver.py
+++ b/examples/pennylane_run_variational_quantum_eigensolver.py
@@ -172,12 +172,13 @@
 Y = np.linspace(-3, 3, 20)
 xx, yy = np.meshgrid(X, Y)
 Z = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))
+
 surf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)
 
 path_z = [cost(var) + 1e-8 for var in var_gd]
 path_x = [v[0] for v in var_gd]
 path_y = [v[1] for v in var_gd]
-ax.plot(path_x, path_y, path_z, c="green", marker=".", label="graddesc")
+ax.plot(path_x, path_y, path_z, c="green", marker=".", label="graddesc", zorder=10)
 
 ax.set_xlabel("v1")
 ax.set_ylabel("v2")
@@ -254,7 +255,7 @@
 path_z = [cost(var) + 1e-8 for var in var_gd]
 path_x = [v[0] for v in var_gd]
 path_y = [v[1] for v in var_gd]
-ax.plot(path_x, path_y, path_z, c="pink", marker=".", label="graddesc")
+ax.plot(path_x, path_y, path_z, c="pink", marker=".", label="graddesc", zorder=10)
 
 ax.set_xlabel("v1")
 ax.set_ylabel("v2")

{"golden_diff": "diff --git a/examples/pennylane_run_variational_quantum_eigensolver.py b/examples/pennylane_run_variational_quantum_eigensolver.py\n--- a/examples/pennylane_run_variational_quantum_eigensolver.py\n+++ b/examples/pennylane_run_variational_quantum_eigensolver.py\n@@ -172,12 +172,13 @@\n Y = np.linspace(-3, 3, 20)\n xx, yy = np.meshgrid(X, Y)\n Z = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))\n+\n surf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)\n \n path_z = [cost(var) + 1e-8 for var in var_gd]\n path_x = [v[0] for v in var_gd]\n path_y = [v[1] for v in var_gd]\n-ax.plot(path_x, path_y, path_z, c=\"green\", marker=\".\", label=\"graddesc\")\n+ax.plot(path_x, path_y, path_z, c=\"green\", marker=\".\", label=\"graddesc\", zorder=10)\n \n ax.set_xlabel(\"v1\")\n ax.set_ylabel(\"v2\")\n@@ -254,7 +255,7 @@\n path_z = [cost(var) + 1e-8 for var in var_gd]\n path_x = [v[0] for v in var_gd]\n path_y = [v[1] for v in var_gd]\n-ax.plot(path_x, path_y, path_z, c=\"pink\", marker=\".\", label=\"graddesc\")\n+ax.plot(path_x, path_y, path_z, c=\"pink\", marker=\".\", label=\"graddesc\", zorder=10)\n \n ax.set_xlabel(\"v1\")\n ax.set_ylabel(\"v2\")\n", "issue": "Missing plot lines in VQE tutorial \n#### Issue description\r\n\r\nThe second and third plots in the VQE tutorial do not show the plot line of the gradient descent. The legends are shown, however. \r\n\r\nIs this problem coming from making this file a \"run\" file? \r\n\r\nhttps://pennylane.readthedocs.io/en/latest/tutorials/pennylane_run_variational_quantum_eigensolver.html#vqe\r\n\r\n* *Expected behavior:*\r\n\r\nThe plots should show the gradient descent on top of the optimization landscape. \r\n\r\n\n", "before_files": [{"content": "r\"\"\"\n.. _vqe:\n\nVariational quantum eigensolver\n===============================\n\nThis example demonstrates the principle of a variational quantum\neigensolver (VQE), originally proposed in `Peruzzo et al.\n(2014) <https://www.nature.com/articles/ncomms5213>`__. To showcase the\nhybrid computational capabilities of PennyLane, we first train a quantum\ncircuit to minimize the squared energy expectation for a Hamiltonian\n:math:`H`,\n\n.. math::\n\n    \\langle \\psi_v | H | \\psi_v \\rangle^2  =( 0.1 \\langle \\psi_{v} | X_2 |\n    \\psi_v \\rangle + 0.5 \\langle \\psi_v | Y_2 | \\psi_v \\rangle )^2.\n\nHere, :math:`|\\psi_v\\rangle` is the state\nobtained after applying a quantum circuit to an initial state\n:math:`|0\\rangle`. The quantum circuit depends on trainable variables\n:math:`v = \\{v_1, v_2\\}`, and :math:`X_2`, :math:`Y_2` denote the\nPauli-X and Pauli-Y operator acting on the second qubit (*Note: We apply\nthe square to make the optimization landscapes more interesting, but in\ncommon applications the cost is directly the energy expectation value*).\n\nAfter doing this, we will then turn things around and use a fixed\nquantum circuit to prepare a state :math:`|\\psi\\rangle`, but train the coefficients of\nthe Hamiltonian to minimize\n\n.. math::\n\n    \\langle \\psi | H | \\psi \\rangle^2  = (v_1 \\langle \\psi | X_2 | \\psi\n    \\rangle + v_2 \\langle \\psi | Y_2 | \\psi \\rangle )^2 .\n\"\"\"\n\n##############################################################################\n# 1. Optimizing the quantum circuit\n# ---------------------------------\n#\n# Imports\n# ~~~~~~~\n#\n# We begin by importing PennyLane, the PennyLane-wrapped version of NumPy,\n# and the GradientDescentOptimizer.\n\nimport pennylane as qml\nfrom pennylane import numpy as np\nfrom pennylane.optimize import GradientDescentOptimizer\n\n##############################################################################\n# We use the default qubit simulator as a device.\n\ndev = qml.device(\"default.qubit\", wires=2)\n\n##############################################################################\n# Quantum nodes\n# ~~~~~~~~~~~~~\n#\n# The quantum circuit of the variational eigensolver is an ansatz that\n# defines a manifold of possible quantum states. We use a Hadamard, two\n# rotations and a CNOT gate to construct our circuit.\n\n\ndef ansatz(var):\n    qml.Rot(0.3, 1.8, 5.4, wires=1)\n    qml.RX(var[0], wires=0)\n    qml.RY(var[1], wires=1)\n    qml.CNOT(wires=[0, 1])\n\n\n##############################################################################\n# A variational eigensolver requires us to evaluate expectations of\n# different Pauli operators. In this example, the Hamiltonian is expressed\n# by only two single-qubit Pauli operators, namely the X and Y operator\n# applied to the first qubit.\n#\n# Since these operators will be measured on the same wire, we will need to\n# create two quantum nodes (one for each operator whose expectation value\n# we measure), but we can reuse the same device.\n#\n# .. note::\n#\n#     If the Pauli observables were evaluated on different wires, we\n#     could use one quantum node and return a tuple of expectations in only\n#     one quantum node:\n#     ``return qml.expectation.PauliX(0), qml.expectation.PauliY(1)``\n\n\n@qml.qnode(dev)\ndef circuit_X(var):\n    ansatz(var)\n    return qml.expval(qml.PauliX(1))\n\n\n@qml.qnode(dev)\ndef circuit_Y(var):\n    ansatz(var)\n    return qml.expval(qml.PauliY(1))\n\n\n##############################################################################\n# Objective\n# ~~~~~~~~~\n\n# The cost function to be optimized in VQE is simply a linear combination\n# of the expectations, which defines the expectation of the Hamiltonian we\n# are interested in. In our case, we square this cost function to provide\n# a more interesting landscape with the same minima.\n\n\ndef cost(var):\n    expX = circuit_X(var)\n    expY = circuit_Y(var)\n    return (0.1 * expX + 0.5 * expY) ** 2\n\n\n##############################################################################\n# This cost defines the following landscape:\n#\n# *Note: To run the following cell you need the matplotlib library.*\n\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib import cm\nfrom matplotlib.ticker import MaxNLocator\n\nfig = plt.figure(figsize=(6, 4))\nax = fig.gca(projection=\"3d\")\n\nX = np.linspace(-3.0, 3.0, 20)\nY = np.linspace(-3.0, 3.0, 20)\nxx, yy = np.meshgrid(X, Y)\nZ = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))\nsurf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)\n\nax.set_xlabel(\"v1\")\nax.set_ylabel(\"v2\")\nax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune=\"lower\"))\n\nplt.show()\n\n##############################################################################\n# Optimization\n# ~~~~~~~~~~~~\n#\n# We create a GradientDescentOptimizer and use it to optimize the cost\n# function.\n\nopt = GradientDescentOptimizer(0.5)\n\nvar = [0.3, 2.5]\nvar_gd = [var]\nfor it in range(20):\n    var = opt.step(cost, var)\n    var_gd.append(var)\n\n    print(\n        \"Cost after step {:5d}: {: .7f} | Variables: [{: .5f},{: .5f}]\".format(\n            it + 1, cost(var), var[0], var[1]\n        )\n    )\n\n##############################################################################\n# We can plot the path that the variables took during gradient descent. To\n# make the plot more clear, we will shorten the range for :math:`v_2`.\n\nfig = plt.figure(figsize=(6, 4))\nax = fig.gca(projection=\"3d\")\n\nX = np.linspace(-3, np.pi / 2, 20)\nY = np.linspace(-3, 3, 20)\nxx, yy = np.meshgrid(X, Y)\nZ = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))\nsurf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)\n\npath_z = [cost(var) + 1e-8 for var in var_gd]\npath_x = [v[0] for v in var_gd]\npath_y = [v[1] for v in var_gd]\nax.plot(path_x, path_y, path_z, c=\"green\", marker=\".\", label=\"graddesc\")\n\nax.set_xlabel(\"v1\")\nax.set_ylabel(\"v2\")\nax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune=\"lower\"))\n\nplt.legend()\nplt.show()\n\n\n##############################################################################\n# 2. Optimizing the Hamiltonian coefficients\n# ------------------------------------------\n#\n# Instead of optimizing the circuit parameters, we can also use a fixed\n# circuit,\n\n\ndef ansatz():\n    qml.Rot(0.3, 1.8, 5.4, wires=1)\n    qml.RX(-0.5, wires=0)\n    qml.RY(0.5, wires=1)\n    qml.CNOT(wires=[0, 1])\n\n\n@qml.qnode(dev)\ndef circuit_X():\n    ansatz()\n    return qml.expval(qml.PauliX(1))\n\n\n@qml.qnode(dev)\ndef circuit_Y():\n    ansatz()\n    return qml.expval(qml.PauliY(1))\n\n\n##############################################################################\n# and make the classical coefficients that appear in the Hamiltonian the\n# trainable variables.\n\n\ndef cost(var):\n    expX = circuit_X()\n    expY = circuit_Y()\n    return (var[0] * expX + var[1] * expY) ** 2\n\n\nopt = GradientDescentOptimizer(0.5)\n\nvar = [0.3, 2.5]\nvar_gd = [var]\nfor it in range(20):\n    var = opt.step(cost, var)\n    var_gd.append(var)\n\n    print(\n        \"Cost after step {:5d}: {: .7f} | Variables: [{: .5f},{: .5f}]\".format(\n            it + 1, cost(var), var[0], var[1]\n        )\n    )\n\n##############################################################################\n# The landscape has a quadratic shape.\n\nfig = plt.figure(figsize=(6, 4))\nax = fig.gca(projection=\"3d\")\n\nX = np.linspace(-3, np.pi / 2, 20)\nY = np.linspace(-3, 3, 20)\nxx, yy = np.meshgrid(X, Y)\nZ = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))\nsurf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)\n\npath_z = [cost(var) + 1e-8 for var in var_gd]\npath_x = [v[0] for v in var_gd]\npath_y = [v[1] for v in var_gd]\nax.plot(path_x, path_y, path_z, c=\"pink\", marker=\".\", label=\"graddesc\")\n\nax.set_xlabel(\"v1\")\nax.set_ylabel(\"v2\")\nax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune=\"lower\"))\n\nplt.legend()\nplt.show()\n\n\n##############################################################################\n# 3. Optimizing classical and quantum parameters\n# ----------------------------------------------\n\n# Finally, we can optimize *classical* and *quantum* weights together by\n# combining the two approaches from above.\n\n\ndef ansatz(var):\n\n    qml.Rot(0.3, 1.8, 5.4, wires=1)\n    qml.RX(var[0], wires=0)\n    qml.RY(var[1], wires=1)\n    qml.CNOT(wires=[0, 1])\n\n\n@qml.qnode(dev)\ndef circuit_X(var):\n    ansatz(var)\n    return qml.expval(qml.PauliX(1))\n\n\n@qml.qnode(dev)\ndef circuit_Y(var):\n    ansatz(var)\n    return qml.expval(qml.PauliY(1))\n\n\ndef cost(var):\n\n    expX = circuit_X(var)\n    expY = circuit_Y(var)\n\n    return (var[2] * expX + var[3] * expY) ** 2\n\n\nopt = GradientDescentOptimizer(0.5)\nvar = [0.3, 2.5, 0.3, 2.5]\n\nfor it in range(10):\n    var = opt.step(cost, var)\n    print(\"Cost after step {:5d}: {: 0.7f}\".format(it + 1, cost(var)))\n", "path": "examples/pennylane_run_variational_quantum_eigensolver.py"}], "after_files": [{"content": "r\"\"\"\n.. _vqe:\n\nVariational quantum eigensolver\n===============================\n\nThis example demonstrates the principle of a variational quantum\neigensolver (VQE), originally proposed in `Peruzzo et al.\n(2014) <https://www.nature.com/articles/ncomms5213>`__. To showcase the\nhybrid computational capabilities of PennyLane, we first train a quantum\ncircuit to minimize the squared energy expectation for a Hamiltonian\n:math:`H`,\n\n.. math::\n\n    \\langle \\psi_v | H | \\psi_v \\rangle^2  =( 0.1 \\langle \\psi_{v} | X_2 |\n    \\psi_v \\rangle + 0.5 \\langle \\psi_v | Y_2 | \\psi_v \\rangle )^2.\n\nHere, :math:`|\\psi_v\\rangle` is the state\nobtained after applying a quantum circuit to an initial state\n:math:`|0\\rangle`. The quantum circuit depends on trainable variables\n:math:`v = \\{v_1, v_2\\}`, and :math:`X_2`, :math:`Y_2` denote the\nPauli-X and Pauli-Y operator acting on the second qubit (*Note: We apply\nthe square to make the optimization landscapes more interesting, but in\ncommon applications the cost is directly the energy expectation value*).\n\nAfter doing this, we will then turn things around and use a fixed\nquantum circuit to prepare a state :math:`|\\psi\\rangle`, but train the coefficients of\nthe Hamiltonian to minimize\n\n.. math::\n\n    \\langle \\psi | H | \\psi \\rangle^2  = (v_1 \\langle \\psi | X_2 | \\psi\n    \\rangle + v_2 \\langle \\psi | Y_2 | \\psi \\rangle )^2 .\n\"\"\"\n\n##############################################################################\n# 1. Optimizing the quantum circuit\n# ---------------------------------\n#\n# Imports\n# ~~~~~~~\n#\n# We begin by importing PennyLane, the PennyLane-wrapped version of NumPy,\n# and the GradientDescentOptimizer.\n\nimport pennylane as qml\nfrom pennylane import numpy as np\nfrom pennylane.optimize import GradientDescentOptimizer\n\n##############################################################################\n# We use the default qubit simulator as a device.\n\ndev = qml.device(\"default.qubit\", wires=2)\n\n##############################################################################\n# Quantum nodes\n# ~~~~~~~~~~~~~\n#\n# The quantum circuit of the variational eigensolver is an ansatz that\n# defines a manifold of possible quantum states. We use a Hadamard, two\n# rotations and a CNOT gate to construct our circuit.\n\n\ndef ansatz(var):\n    qml.Rot(0.3, 1.8, 5.4, wires=1)\n    qml.RX(var[0], wires=0)\n    qml.RY(var[1], wires=1)\n    qml.CNOT(wires=[0, 1])\n\n\n##############################################################################\n# A variational eigensolver requires us to evaluate expectations of\n# different Pauli operators. In this example, the Hamiltonian is expressed\n# by only two single-qubit Pauli operators, namely the X and Y operator\n# applied to the first qubit.\n#\n# Since these operators will be measured on the same wire, we will need to\n# create two quantum nodes (one for each operator whose expectation value\n# we measure), but we can reuse the same device.\n#\n# .. note::\n#\n#     If the Pauli observables were evaluated on different wires, we\n#     could use one quantum node and return a tuple of expectations in only\n#     one quantum node:\n#     ``return qml.expectation.PauliX(0), qml.expectation.PauliY(1)``\n\n\n@qml.qnode(dev)\ndef circuit_X(var):\n    ansatz(var)\n    return qml.expval(qml.PauliX(1))\n\n\n@qml.qnode(dev)\ndef circuit_Y(var):\n    ansatz(var)\n    return qml.expval(qml.PauliY(1))\n\n\n##############################################################################\n# Objective\n# ~~~~~~~~~\n\n# The cost function to be optimized in VQE is simply a linear combination\n# of the expectations, which defines the expectation of the Hamiltonian we\n# are interested in. In our case, we square this cost function to provide\n# a more interesting landscape with the same minima.\n\n\ndef cost(var):\n    expX = circuit_X(var)\n    expY = circuit_Y(var)\n    return (0.1 * expX + 0.5 * expY) ** 2\n\n\n##############################################################################\n# This cost defines the following landscape:\n#\n# *Note: To run the following cell you need the matplotlib library.*\n\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib import cm\nfrom matplotlib.ticker import MaxNLocator\n\nfig = plt.figure(figsize=(6, 4))\nax = fig.gca(projection=\"3d\")\n\nX = np.linspace(-3.0, 3.0, 20)\nY = np.linspace(-3.0, 3.0, 20)\nxx, yy = np.meshgrid(X, Y)\nZ = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))\nsurf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)\n\nax.set_xlabel(\"v1\")\nax.set_ylabel(\"v2\")\nax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune=\"lower\"))\n\nplt.show()\n\n##############################################################################\n# Optimization\n# ~~~~~~~~~~~~\n#\n# We create a GradientDescentOptimizer and use it to optimize the cost\n# function.\n\nopt = GradientDescentOptimizer(0.5)\n\nvar = [0.3, 2.5]\nvar_gd = [var]\nfor it in range(20):\n    var = opt.step(cost, var)\n    var_gd.append(var)\n\n    print(\n        \"Cost after step {:5d}: {: .7f} | Variables: [{: .5f},{: .5f}]\".format(\n            it + 1, cost(var), var[0], var[1]\n        )\n    )\n\n##############################################################################\n# We can plot the path that the variables took during gradient descent. To\n# make the plot more clear, we will shorten the range for :math:`v_2`.\n\nfig = plt.figure(figsize=(6, 4))\nax = fig.gca(projection=\"3d\")\n\nX = np.linspace(-3, np.pi / 2, 20)\nY = np.linspace(-3, 3, 20)\nxx, yy = np.meshgrid(X, Y)\nZ = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))\n\nsurf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)\n\npath_z = [cost(var) + 1e-8 for var in var_gd]\npath_x = [v[0] for v in var_gd]\npath_y = [v[1] for v in var_gd]\nax.plot(path_x, path_y, path_z, c=\"green\", marker=\".\", label=\"graddesc\", zorder=10)\n\nax.set_xlabel(\"v1\")\nax.set_ylabel(\"v2\")\nax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune=\"lower\"))\n\nplt.legend()\nplt.show()\n\n\n##############################################################################\n# 2. Optimizing the Hamiltonian coefficients\n# ------------------------------------------\n#\n# Instead of optimizing the circuit parameters, we can also use a fixed\n# circuit,\n\n\ndef ansatz():\n    qml.Rot(0.3, 1.8, 5.4, wires=1)\n    qml.RX(-0.5, wires=0)\n    qml.RY(0.5, wires=1)\n    qml.CNOT(wires=[0, 1])\n\n\n@qml.qnode(dev)\ndef circuit_X():\n    ansatz()\n    return qml.expval(qml.PauliX(1))\n\n\n@qml.qnode(dev)\ndef circuit_Y():\n    ansatz()\n    return qml.expval(qml.PauliY(1))\n\n\n##############################################################################\n# and make the classical coefficients that appear in the Hamiltonian the\n# trainable variables.\n\n\ndef cost(var):\n    expX = circuit_X()\n    expY = circuit_Y()\n    return (var[0] * expX + var[1] * expY) ** 2\n\n\nopt = GradientDescentOptimizer(0.5)\n\nvar = [0.3, 2.5]\nvar_gd = [var]\nfor it in range(20):\n    var = opt.step(cost, var)\n    var_gd.append(var)\n\n    print(\n        \"Cost after step {:5d}: {: .7f} | Variables: [{: .5f},{: .5f}]\".format(\n            it + 1, cost(var), var[0], var[1]\n        )\n    )\n\n##############################################################################\n# The landscape has a quadratic shape.\n\nfig = plt.figure(figsize=(6, 4))\nax = fig.gca(projection=\"3d\")\n\nX = np.linspace(-3, np.pi / 2, 20)\nY = np.linspace(-3, 3, 20)\nxx, yy = np.meshgrid(X, Y)\nZ = np.array([[cost([x, y]) for x in X] for y in Y]).reshape(len(Y), len(X))\nsurf = ax.plot_surface(xx, yy, Z, cmap=cm.coolwarm, antialiased=False)\n\npath_z = [cost(var) + 1e-8 for var in var_gd]\npath_x = [v[0] for v in var_gd]\npath_y = [v[1] for v in var_gd]\nax.plot(path_x, path_y, path_z, c=\"pink\", marker=\".\", label=\"graddesc\", zorder=10)\n\nax.set_xlabel(\"v1\")\nax.set_ylabel(\"v2\")\nax.zaxis.set_major_locator(MaxNLocator(nbins=5, prune=\"lower\"))\n\nplt.legend()\nplt.show()\n\n\n##############################################################################\n# 3. Optimizing classical and quantum parameters\n# ----------------------------------------------\n\n# Finally, we can optimize *classical* and *quantum* weights together by\n# combining the two approaches from above.\n\n\ndef ansatz(var):\n\n    qml.Rot(0.3, 1.8, 5.4, wires=1)\n    qml.RX(var[0], wires=0)\n    qml.RY(var[1], wires=1)\n    qml.CNOT(wires=[0, 1])\n\n\n@qml.qnode(dev)\ndef circuit_X(var):\n    ansatz(var)\n    return qml.expval(qml.PauliX(1))\n\n\n@qml.qnode(dev)\ndef circuit_Y(var):\n    ansatz(var)\n    return qml.expval(qml.PauliY(1))\n\n\ndef cost(var):\n\n    expX = circuit_X(var)\n    expY = circuit_Y(var)\n\n    return (var[2] * expX + var[3] * expY) ** 2\n\n\nopt = GradientDescentOptimizer(0.5)\nvar = [0.3, 2.5, 0.3, 2.5]\n\nfor it in range(10):\n    var = opt.step(cost, var)\n    print(\"Cost after step {:5d}: {: 0.7f}\".format(it + 1, cost(var)))\n", "path": "examples/pennylane_run_variational_quantum_eigensolver.py"}]}