I cannot obtain the second-order approximation of exp(-itU). This is my code:
def v0(t):
"""Calculates the first column of the PREPARE matrix, v0.
Args:
t (float): the time we evolve for.
Returns:
(array): v0 = [v00, v01, v02, v03] normalized
"""
##################
# YOUR CODE HERE #
##################
v00 = 1
v01 = np.sqrt(t)
v02 = t/np.sqrt(2)
v03 = 0
return np.array([v00, v01, v02, v03])/np.sqrt(1+t+1/2*t**2)
def exp_U_second(unitaries, t):
"""Implements the second-order approximation to Hamiltonian time evolution.
Args:
unitaries (list): A list containing 4 unitary operations in this order:
(U0, U1, U2, U3).
t (float): The Hamiltonian evolution time.
"""
##################
# YOUR CODE HERE #
##################
qml.MottonenStatePreparation(state_vector=v0(t),wires=range(2))
select(unitaries)
qml.MottonenStatePreparation(state_vector=np.transpose(np.conjugate(v0(t))),wires=range(2))
Hey @Dhawal_Verma!
I recommend trying to use your prepare
function that you had to have completed in H.9.4a. Make sure you use qml.adjoint
when applicable! Let me know if this helps
Hi I usedprepare in this code. My former code in which I have to used qml.adjoint, I replaced it with np.transpose(np.conjugate(A)) and it worked every time. Please reply. Thanks.
This is little modification but it is still not working. Am I really vey far way from the true solution.
def v0(t):
“”"Calculates the first column of the PREPARE matrix, v0.
Args:
t (float): the time we evolve for.
Returns:
(array): v0 = [v00, v01, v02, v03] normalized
“”"
I didn’t see anything wrong with your v0
function:
def v0(t):
"""Calculates the first column of the PREPARE matrix, v0.
Args:
t (float): the time we evolve for.
Returns:
(array): v0 = [v00, v01, v02, v03] normalized
"""
##################
# YOUR CODE HERE #
##################
v00 = 1
v01 = np.sqrt(t)
v02 = t/np.sqrt(2)
v03 = 0
return np.array([v00, v01, v02, v03])/np.sqrt(1+t+1/2*t**2)
But your exp_U_second
function isn’t right. The algorithm is PREP, SELECT, then PREP ^\dagger, and your prepare
function should be this (or very similar) from H.6.4a:
def prepare(coeffs):
qml.MottonenStatePreparation(normalize(coeffs), wires=aux)
Hope this helps!
1 Like
Thanks for this. I wrote my code for exp_U_second as, after the modification,
def exp_U_second(unitaries, t):
prepare(v0(t))
select(unitaries)
qml.adjoint(prepare(v0(t)))
I am getting ans error as: “The object None of type <class ‘NoneType’> is not callable. This error might occur if you apply adjoint to a list of operations instead of a function or template.”
Sorry for bothering you but I think it is very close. Please help me.
Hi. After giving it some try, I succeeded. Please ignore the former message. By the way, this worked “qml.adjoint(qml.MottonenStatePreparation(normalize(v0(t)),wires=[0,1]))”
2 Likes
Awesome! Glad to hear
We have a new PennyLane survey . Let us know your thoughts about PennyLane so that we can keep bringing you amazing features .