diff --git a/challenges/xanadu challenge/challenge_template.py b/challenges/xanadu challenge/challenge_template.py index 7018c54..672e09d 100644 --- a/challenges/xanadu challenge/challenge_template.py +++ b/challenges/xanadu challenge/challenge_template.py @@ -52,11 +52,24 @@ 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 + # Setting up a seed for the random numbers of the parameters + np.random.seed(12) - return # 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) + # Initializing the optimizer object + opt = qml.GradientDescentOptimizer(stepsize=5e-2) + + 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 + + + return variational_circuit(params, hamiltonian).eigvals()[0] # Return the value of the minimized QNode diff --git a/challenges/xanadu challenge/readme.md b/challenges/xanadu challenge/readme.md new file mode 100644 index 0000000..4aa5587 --- /dev/null +++ b/challenges/xanadu challenge/readme.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