Hello! If applicable, put your complete code example down below. Make sure that your code:
is 100% self-contained — someone can copy-paste exactly what is here and run it to
reproduce the behaviour you are observing
includes comments
# Put code here
```import json
import pennylane as qml
import pennylane.numpy as np
dev = qml.device('default.qubit', wires = 2)
@qml.qnode(dev)
def trotterize(alpha, beta, time, depth):
"""This quantum circuit implements the Trotterization of a Hamiltonian given by a linear combination
of tensor products of X and Z Pauli gates.
Args:
alpha (float): The coefficient of the XX term in the Hamiltonian, as in the statement of the problem.
beta (float): The coefficient of the YY term in the Hamiltonian, as in the statement of the problem.
time (float): Time interval during which the quantum state evolves under the interactions specified by the Hamiltonian.
depth (int): The Trotterization depth.
Returns:
(numpy.array): The probabilities of measuring each computational basis state.
"""
# Put your code here #
H = alpha * qml.PauliX(0) @ qml.PauliX(1) + beta * qml.PauliZ(0) @ qml.PauliZ(1)
U = qml.ApproxTimeEvolution(H, time, depth)
# Return the probabilities
return qml.probs(wires=[0, 1])
#evolved_circuit = qml.evolve(trotterize, H, time, depth)
If you want help with diagnosing an error, please put the full error message below:
Put full error message here
Running on public test set
Test 1 of 2 failed! 🚫
Input: [0.9,1.0,0.4,2]
Expected output: [0.87590286, 0, 0, 0.12409714]
Solution output: [0.0, 0.8759028645704459, 0.12409713542955236, 0.0]
Failure message: Assertion Error: Your circuit does not give the correct probabilities..
And, finally, make sure to include the versions of your packages. Specifically, show us the output of `qml.about()`.
This may arise from a problem with how you define the Hamiltonian. The better practice is to use qml.dot (see here). However, do note that the exercise forbids the use of ApproxTimeEvolution. Try to do it another way!
import pennylane as qml
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def trotterize(alpha, beta, time, depth):
"""This quantum circuit implements the Trotterization of a Hamiltonian given by a linear combination
of tensor products of X and Z Pauli gates.
Args:
alpha (float): The coefficient of the XX term in the Hamiltonian, as in the statement of the problem.
beta (float): The coefficient of the YY term in the Hamiltonian, as in the statement of the problem.
time (float): Time interval during which the quantum state evolves under the interactions specified by the Hamiltonian.
depth (int): The Trotterization depth.
Returns:
(numpy.array): The probabilities of measuring each computational basis state.
"""
for _ in range(depth):
qml.IsingXX(alpha * time / depth, wires=[0, 1])
qml.IsingZZ(beta * time / depth, wires=[0, 1])
return qml.probs(wires=[0, 1])
Running on public test set
Test 1 of 2 failed! 🚫
Input: [0.9,1.0,0.4,2]
Expected output: [0.87590286, 0, 0, 0.12409714]
Solution output: [0.9679484118389678, 0.0, 0.0, 0.032051588161032575]
Failure message: Assertion Error: Your circuit does not give the correct probabilities..
Test 2 of 2 failed! 🚫
Input: [0.5,0.8,0.2,1]
Expected output: [0.99003329, 0, 0, 0.00996671]
Solution output: [0.997502082639013, 0.0, 0.0, 0.0024979173609871175]
Failure message: Assertion Error: Your circuit does not give the correct probabilities..
Running on private test set
One or more private tests failed. Try again! 🚫
Hi, @thomas.ehmer, how are you doing?
It turned out we need 2 * constant_currently_using for both IsingXX and IsingZZ.
If you could figure out why it works that way, would you kindly please let me know?
Good luck on your journey
As to why … … but, now I asked my good friend who knows maths and this is the reply:
The reason why you need to use 2*depth instead of just depth in the Trotterization loop is because of the way the Trotterization approximation is constructed.
The Trotterization approximation for the time evolution operator U(t) is given by:
U(t) ≈ (e^(-iαtX⊗X/n) * e^(-iβtZ⊗Z/n))^n
Here, n is the Trotterization depth.
However, in each iteration of the loop, we are applying the two exponentials e^(-iαtX⊗X/n) and e^(-iβtZ⊗Z/n) sequentially. Therefore, to achieve the desired approximation, we need to repeat this sequence n times.
In other words, if we loop over range(depth), we are effectively applying the sequence (e^(-iαtX⊗X/n) * e^(-iβtZ⊗Z/n)) only depth times, which is not the correct Trotterization approximation.
By looping over range(2*depth), we are applying the sequence (e^(-iαtX⊗X/n) * e^(-iβtZ⊗Z/n)) a total of 2*depth times, which is equivalent to applying the desired approximation (e^(-iαtX⊗X/n) * e^(-iβtZ⊗Z/n))^n.
This is because each iteration of the loop applies the two exponentials once, and we need to repeat this sequence n times to achieve the correct Trotterization approximation.