Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. def test_nonzero_infidelity_gradient_with_degenerate_hamiltonian():
  2.     """
  3.    Tests that the infidelity gradient is non-zero for a degenerate non-trivial noisy system.
  4.    """
  5.     with tf.Graph().as_default() as g:
  6.         pulse = tf.compat.v1.placeholder(shape=(2,), dtype=tf.float64)
  7.         hamiltonian_pwc = TensorPwc([1, 1],
  8.                                     (tf.cast(pulse[:, None, None], dtype=tf.complex128)
  9.                                      * tf.constant(np.array([np.diag([1, 1, 2, 2]),
  10.                                                              np.diag([1, 1, 2, 2])]),
  11.                                                    dtype=tf.complex128)))
  12.         target = Target(np.eye(4))
  13.  
  14.         infidelity = create_infidelity(hamiltonian_pwc=hamiltonian_pwc,
  15.                                        noise_operator_pwcs=[],
  16.                                        target=target)
  17.  
  18.         noise_operator_pwc = TensorPwc([2], tf.constant(np.diag([0, 0, 1, 1])[None],
  19.                                                         dtype=tf.complex128))
  20.         infidelity_noise = create_infidelity(hamiltonian_pwc=hamiltonian_pwc,
  21.                                              noise_operator_pwcs=[noise_operator_pwc],
  22.                                              target=target)
  23.  
  24.         grad = tf.gradients(infidelity, pulse)[0]
  25.         grad_noise = tf.gradients(infidelity_noise, pulse)[0]
  26.  
  27.         with tf.compat.v1.Session() as session:
  28.             grad_value = session.run(grad, feed_dict={pulse: np.array([2, 1])})
  29.             grad_noise_value = session.run(grad_noise, feed_dict={pulse: np.array([2, 1])})
  30.     # Changing the pulse should affect the infidelities, so the gradients should be non-zero.
  31.     assert not np.any(np.isnan(grad_value))
  32.     assert not np.allclose(grad_value, 0.)
  33.     assert not np.any(np.isnan(grad_noise_value))
  34.  NORMAL  ᚠ deg_fix!  tests/test_cost.py
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement