Consider the single-qubit Hamiltonian

The commutator is proportional to , and one can show by expanding the exponential as a Taylor series and rearranging odd- and even-power terms that

Let's write a function to compute the error : the norm of the difference between the Trotterized result and the exact result .

Complete the code for trotter_XandZ below to Trotterize evolution with the Hamiltonian . The exact result exact_result_XandZ is provided for you. Submitting will generate a plot of the error trotter_error_XandZ against the number of steps on a log-log scale for two different choices of and .

Note that the error is related to the number of steps via

or, in the logarithmic scale, Therefore, the log-log plot should be a line with slope . Is this what you see?

dev = qml.device("default.qubit", wires=1)

def exact_result_XandZ(alpha, beta, time):
"""Exact circuit for evolving a qubit with H = alpha Z + beta X.
Args:
alpha (float): The coefficient of Z in the Hamiltonian.
beta (float): The coefficient of X in the Hamiltonian.
time (float): The time we evolve the state for.
Returns:
array[complex]: The exact state after evolution.
"""
root = np.sqrt(alpha**2 + beta**2)
c_0 = np.cos(root*time) - (alpha/root)*np.sin(root*time)*1.j
c_1 = -(beta/root)*np.sin(root*time)*1.j
return np.array([c_0, c_1])
@qml.qnode(dev)
def trotter_XandZ(alpha, beta, time, n):
"""Trotterized circuit for evolving a qubit with H = alpha Z + beta X.
Args:
alpha (float): The coefficient of Z in the Hamiltonian.
beta (float): The coefficient of X in the Hamiltonian.
time (float): The time we evolve the state for.
n (int): The number of steps in our Trotterization.
Returns:
array[complex]: The state after applying the Trotterized circuit.
"""
##################
# YOUR CODE HERE #
##################

return qml.state()

or to submit your code

To interact with codercises, please switch to a larger screen size.

Learning Objectives:

  • Apply the Zassenhaus formula to Trotterization errors and higher-order formulas.
  • Describe how the desired error size influences the gate count