From 0a691e44fc51733d6c51255f77a404661302e411 Mon Sep 17 00:00:00 2001 From: Brian Navarro Date: Wed, 8 Nov 2023 20:15:54 -0600 Subject: [PATCH 1/5] Adding the optimizer to the variational circuit --- .../xanadu challenge/challenge_template.py | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/challenges/xanadu challenge/challenge_template.py b/challenges/xanadu challenge/challenge_template.py index 7018c54..465a943 100644 --- a/challenges/xanadu challenge/challenge_template.py +++ b/challenges/xanadu challenge/challenge_template.py @@ -52,11 +52,26 @@ def optimize_circuit(hamiltonian): ### Solution Template - dev = # Initialize the device. + dev = qml.device("default.qubit", wires=WIRES, shots=50)# Initialize the device. - circuit = # Instantiate the QNode from variational_circuit. + circuit = qml.QNode(variational_circuit, dev)# Instantiate the QNode from variational_circuit. # Write your code to minimize the circuit + np.random.seed(12) + params = np.random.randn(1, 30) + opt = qml.GradientDescentOptimizer(stepsize=5e-2) + for i in range(51): + params, cost = opt.step_and_cost(circuit, params) + if i % 10 == 0: + print(f"Step {i}: cost = {cost:.4f}") + + + return variational_circuit(params)# Return the value of the minimized QNode + +hamiltonian = [0.863327072347624,0.0167108057202516,0.07991447085492759,0.0854049026262154,0.0167108057202516,0.8237963773906136, + -0.07695947154193797,0.03131548733285282,0.07991447085492759,-0.07695947154193795,0.8355417021014687, + -0.11345916130631205,0.08540490262621539,0.03131548733285283,-0.11345916130631205,0.758156886827099] - return # Return the value of the minimized QNode +result = optimize_circuit(hamiltonian) +print(result.eigvals()[0]) From 99a7b83eafe7a031507aac1c2680bdc16742adbc Mon Sep 17 00:00:00 2001 From: Brian Navarro Date: Wed, 8 Nov 2023 21:05:51 -0600 Subject: [PATCH 2/5] Added kwargs --- challenges/xanadu challenge/challenge_template.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/challenges/xanadu challenge/challenge_template.py b/challenges/xanadu challenge/challenge_template.py index 465a943..2465999 100644 --- a/challenges/xanadu challenge/challenge_template.py +++ b/challenges/xanadu challenge/challenge_template.py @@ -61,16 +61,16 @@ def optimize_circuit(hamiltonian): params = np.random.randn(1, 30) opt = qml.GradientDescentOptimizer(stepsize=5e-2) for i in range(51): - params, cost = opt.step_and_cost(circuit, params) + params, cost = opt.step_and_cost(circuit, params, hamiltonian=hamiltonian) if i % 10 == 0: print(f"Step {i}: cost = {cost:.4f}") - return variational_circuit(params)# Return the value of the minimized QNode + return variational_circuit(params, hamiltonian)# Return the value of the minimized QNode -hamiltonian = [0.863327072347624,0.0167108057202516,0.07991447085492759,0.0854049026262154,0.0167108057202516,0.8237963773906136, - -0.07695947154193797,0.03131548733285282,0.07991447085492759,-0.07695947154193795,0.8355417021014687, - -0.11345916130631205,0.08540490262621539,0.03131548733285283,-0.11345916130631205,0.758156886827099] +hamiltonian = [0.32158897156285354,-0.20689268438270836,0.12366748295758379,-0.11737425017261123,-0.20689268438270836, + 0.7747346055276305,-0.05159966365446514,0.08215539696259792,0.12366748295758379,-0.05159966365446514, + 0.5769050487087416,0.3853362904758938,-0.11737425017261123,0.08215539696259792,0.3853362904758938,0.3986256655167206] result = optimize_circuit(hamiltonian) From aa66fb95ecafef334dfcb695a353c7d5d7f8b8b2 Mon Sep 17 00:00:00 2001 From: Brian Navarro Date: Wed, 8 Nov 2023 23:52:44 -0600 Subject: [PATCH 3/5] Reformating the code to fit the challenge petitions --- .../xanadu challenge/challenge_template.py | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/challenges/xanadu challenge/challenge_template.py b/challenges/xanadu challenge/challenge_template.py index 2465999..672e09d 100644 --- a/challenges/xanadu challenge/challenge_template.py +++ b/challenges/xanadu challenge/challenge_template.py @@ -52,26 +52,24 @@ def optimize_circuit(hamiltonian): ### Solution Template - dev = qml.device("default.qubit", wires=WIRES, shots=50)# Initialize the device. + dev = qml.device("default.qubit", wires=WIRES, shots=50) # Initialize the device. - circuit = qml.QNode(variational_circuit, dev)# Instantiate the QNode from variational_circuit. + circuit = qml.QNode(variational_circuit, dev) # Instantiate the QNode from variational_circuit. # Write your code to minimize the circuit + # Setting up a seed for the random numbers of the parameters np.random.seed(12) - params = np.random.randn(1, 30) - opt = qml.GradientDescentOptimizer(stepsize=5e-2) - for i in range(51): - params, cost = opt.step_and_cost(circuit, params, hamiltonian=hamiltonian) - if i % 10 == 0: - print(f"Step {i}: cost = {cost:.4f}") - - return variational_circuit(params, hamiltonian)# Return the value of the minimized QNode + # Generating a random set of params with the size required for the challenge + params = np.random.randn(1, 30) -hamiltonian = [0.32158897156285354,-0.20689268438270836,0.12366748295758379,-0.11737425017261123,-0.20689268438270836, - 0.7747346055276305,-0.05159966365446514,0.08215539696259792,0.12366748295758379,-0.05159966365446514, - 0.5769050487087416,0.3853362904758938,-0.11737425017261123,0.08215539696259792,0.3853362904758938,0.3986256655167206] + # Initializing the optimizer object + opt = qml.GradientDescentOptimizer(stepsize=5e-2) -result = optimize_circuit(hamiltonian) + for i in range(51): # Starting a 50 step minimization + params, cost = opt.step_and_cost(circuit, params, hamiltonian=hamiltonian) # Doing a step with the current params and + if i % 10 == 0: # returning the new set of params + print(f"Step {i}: cost = {cost:.4f}") # Print the cost value every 10 steps + -print(result.eigvals()[0]) + return variational_circuit(params, hamiltonian).eigvals()[0] # Return the value of the minimized QNode From a9e9bab97ad552dc4cb9d7338a018d0600630064 Mon Sep 17 00:00:00 2001 From: Brian Navarro Date: Thu, 9 Nov 2023 23:27:04 -0600 Subject: [PATCH 4/5] Summary of our work --- .../xanadu challenge/solution_summary.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 challenges/xanadu challenge/solution_summary.md diff --git a/challenges/xanadu challenge/solution_summary.md b/challenges/xanadu challenge/solution_summary.md new file mode 100644 index 0000000..4aa5587 --- /dev/null +++ b/challenges/xanadu challenge/solution_summary.md @@ -0,0 +1,27 @@ +# Xanadu challenge + +Rotation Latin team + +- Nayeli Rodriguez +- Anthony Córdoba +- Alexa Gonzalez +- María Rojas +- Brian Navarro + +#### Methodology + +After reading the whole challenge and understanding what was needed to work on, we started to research a gradient function that could work for our case and came across this [blog](https://pennylane.ai/blog/2022/06/how-to-choose-your-optimizer/) pointing out some functions that Pennylane offers and different applications they were better at. We chose the *GradientDescentOptimizer* which was an optimizer we heard before and considered could work on many cases. We went this route instead of developing our own optimizer due to a lack of expertise on the subject and the constraints of time. + +After selecting our optimizer, we researched the Pennylane API reference to understand how to implement it in our code. While testing our code we found out about the *QNSPSAOptimizer* class which had an example of use, so we decided to base our implementation around that example but using our previously selected optimizer. + +We also used the examples shown in the Xanadu Codebook on how to Instantiate the QNode for the variational circuit. It was a very useful resource to make our code work properly. + +We had some problems when trying to run the optimizer due to our lack of how the arguments of a callable function inside another function parameter works. We sorted it out with the use of the kwargs and researched in the API reference how to obtain the information we needed from the object returned from the variational circuit. Afterward, we created a new script to call our code as an imported object (which probably will be the case when Xanadu judges test our solution) to test it out with the Hamiltonians provided by the challenge. + +#### Results + +We were able to obtain the same value as the expected one from the Hamiltonians provided by the challenge. We also tested it with another optimizer called *AdagradOptimizer* obtaining the same result. We also tweaked the number of steps for the optimizer and shots from our device and found out that the current solution has better results on precision, accuracy, and execution time, but can vary depending on the hardware used to run the solution. + +#### Conclusions + +We can conclude that Pennylane is a very powerful tool for simulating quantum devices and can be used properly without extensive knowledge of quantum computing; knowing the basics, having good skills in python, and experience working with APIs were sufficient for us to develop this solution. \ No newline at end of file From 34ea7c486969ebc5440c0d36bfc9893a1f1e11d7 Mon Sep 17 00:00:00 2001 From: Brian Navarro Date: Thu, 9 Nov 2023 23:29:12 -0600 Subject: [PATCH 5/5] Rename md file to be able to display by default in GitHub --- challenges/xanadu challenge/{solution_summary.md => readme.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename challenges/xanadu challenge/{solution_summary.md => readme.md} (100%) diff --git a/challenges/xanadu challenge/solution_summary.md b/challenges/xanadu challenge/readme.md similarity index 100% rename from challenges/xanadu challenge/solution_summary.md rename to challenges/xanadu challenge/readme.md