Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. import unittest
  2.  
  3. class TestClass(unittest.Testcase):
  4.  
  5.     def setUp(self):
  6.         pass
  7.  
  8.     def test_nonzero_infidelity_gradient_with_degenerate_hamiltonian():
  9.         """
  10.         Tests that the infidelity gradient is non-zero for a degenerate non-trivial noisy system.
  11.         """
  12.         with tf.Graph().as_default() as g:
  13.             pulse = tf.compat.v1.placeholder(shape=(2,), dtype=tf.float64)
  14.             hamiltonian_pwc = TensorPwc([1, 1],
  15.                                         (tf.cast(pulse[:, None, None], dtype=tf.complex128)
  16.                                          * tf.constant(np.array([np.diag([1, 1, 2, 2]),
  17.                                                                  np.diag([1, 1, 2, 2])]),
  18.                                                        dtype=tf.complex128)))
  19.             target = Target(np.eye(4))
  20.  
  21.             infidelity = create_infidelity(hamiltonian_pwc=hamiltonian_pwc,
  22.                                            noise_operator_pwcs=[],
  23.                                            target=target)
  24.  
  25.             noise_operator_pwc = TensorPwc([2], tf.constant(np.diag([0, 0, 1, 1])[None],
  26.                                                             dtype=tf.complex128))
  27.             infidelity_noise = create_infidelity(hamiltonian_pwc=hamiltonian_pwc,
  28.                                                  noise_operator_pwcs=[noise_operator_pwc],
  29.                                                  target=target)
  30.  
  31.             grad = tf.gradients(infidelity, pulse)[0]
  32.             grad_noise = tf.gradients(infidelity_noise, pulse)[0]
  33.  
  34.             with tf.compat.v1.Session() as session:
  35.                 grad_value = session.run(grad, feed_dict={pulse: np.array([2, 1])})
  36.                 grad_noise_value = session.run(grad_noise, feed_dict={pulse: np.array([2, 1])})
  37.  
  38.         # Changing the pulse should affect the infidelities, so the gradients should be non-zero.
  39.         self.assertNotIn(np.Nan, grad_value)
  40.         for val in grad_value:
  41.             self.assertAlmostEqual(val, 0., delta=0.001)
  42.         self.assertNotIn(np.NaN, grad_noise_value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement