def test_signal(self): """Test Signal.""" # Signal with constant amplitude signal = Signal(0.25, carrier_freq=0.3) self.assertEqual(signal.envelope_value(), 0.25) self.assertEqual(signal.envelope_value(1.23), 0.25) self.assertEqual(signal.value(), 0.25) self.assertEqual(signal.value(1.0), 0.25 * np.exp(0.3 * 2.0j * np.pi)) signal = Signal(0.25, carrier_freq=0.3, phase=0.5) self.assertEqual(signal.value(1.0), 0.25 * np.exp(0.3 * 2.0j * np.pi + 0.5j)) # Signal with parabolic amplitude signal = Signal(lambda t: 2.0 * t**2, carrier_freq=0.1) self.assertEqual(signal.envelope_value(), 0.0) self.assertEqual(signal.envelope_value(3.0), 18.0) self.assertEqual(signal.value(), 0.0) self.assertEqual(signal.value(2.0), 8.0 * np.exp(0.1 * 2.0j * np.pi * 2.0)) signal = Signal(lambda t: 2.0 * t**2, carrier_freq=0.1, phase=-0.1) self.assertEqual(signal.value(2.0), 8.0 * np.exp(0.1 * 2.0j * np.pi * 2.0 - 0.1j))
def test_addition(self): """Tests the multiplication of signals.""" # Test Constant const1 = Constant(0.3) const2 = Constant(0.5) self.assertTrue(isinstance(const1 + const2, Signal)) self.assertEqual((const1 + const2).value(), 0.8) # Test Signal signal1 = Signal(3.0, carrier_freq=0.1) signal2 = Signal(lambda t: 2.0 * t ** 2, carrier_freq=0.1) self.assertTrue(isinstance(const1 + signal1, Signal)) self.assertTrue(isinstance(signal1 + const1, Signal)) self.assertTrue(isinstance(signal1 + signal2, Signal)) self.assertEqual((signal1 + signal2).carrier_freq, 0.0) self.assertEqual((signal1 + const1).carrier_freq, 0.0) self.assertEqual((signal1 + signal2).envelope_value(), 3.0) expected = 21.0 * np.cos(0.1 * 2.0 * np.pi * 3.0) self.assertAlmostEqual((signal1 + signal2).envelope_value(3.0), expected, places=8) self.assertEqual((signal1 + signal2).value(), 3.0) self.assertEqual((signal1 + signal2).value(2.0), 11.0 * np.cos(0.1 * 2.0 * np.pi * 2.0)) # Test piecewise constant dt = 1.0 samples = Array([0.0, 0.0, 1.0, 2.0, 1.0, 0.0, 0.0]) carrier_freq = 0.5 pwc1 = PiecewiseConstant(dt=dt, samples=samples, carrier_freq=carrier_freq) dt = 1.0 samples = Array([0.0, 0.0, 1.0, 2.0, 1.0, 0.0, 0.0]) carrier_freq = 0.1 pwc2 = PiecewiseConstant(dt=dt, samples=samples, carrier_freq=carrier_freq) # Test types self.assertTrue(isinstance(const1 + pwc1, Signal)) self.assertTrue(isinstance(signal1 + pwc1, Signal)) self.assertTrue(isinstance(pwc1 + pwc2, Signal)) self.assertTrue(isinstance(pwc1 + const1, Signal)) self.assertTrue(isinstance(pwc1 + signal1, Signal)) # Test values self.assertEqual((pwc1 + pwc2).carrier_freq, 0.0) self.assertEqual((pwc1 + pwc2).envelope_value(), 0.0) expected = 1.0 * np.cos(0.5 * 2.0 * np.pi * 4.0) + 1.0 * np.cos(0.1 * 2.0 * np.pi * 4.0) self.assertAlmostEqual((pwc1 + pwc2).envelope_value(4.0), expected, places=8) self.assertEqual((pwc1 + pwc2).value(), 0.0) expected = 1.0 * np.cos(0.5 * 2.0 * np.pi * 4.0) + 1.0 * np.cos(0.1 * 2.0 * np.pi * 4.0) self.assertAlmostEqual((pwc1 + pwc2).value(4.0), expected, places=8) # Test phase pwc2 = PiecewiseConstant(dt=dt, samples=samples, carrier_freq=carrier_freq, phase=0.5) expected = 1.0 * np.cos(0.5 * 2.0 * np.pi * 4.0) + 1.0 * np.cos( 0.1 * 2.0 * np.pi * 4.0 + 0.5 ) self.assertAlmostEqual((pwc1 + pwc2).envelope_value(4.0), expected, places=8)
def test_multiplication(self): """Tests the multiplication of signals.""" # Test Constant const1 = Constant(0.3) const2 = Constant(0.5) self.assertTrue(isinstance(const1 * const2, Signal)) self.assertEqual((const1 * const2).value(), 0.15) self.assertEqual((const1 * const2).value(10.0), 0.15) # Test Signal signal1 = Signal(3.0, carrier_freq=0.1) signal2 = Signal(lambda t: 2.0 * t ** 2, carrier_freq=0.1) self.assertTrue(isinstance(const1 * signal1, Signal)) self.assertTrue(isinstance(signal1 * const1, Signal)) self.assertTrue(isinstance(signal1 * signal2, Signal)) self.assertEqual((signal1 * signal2).carrier_freq, 0.2) self.assertEqual((signal1 * const1).carrier_freq, 0.1) self.assertEqual((signal1 * signal2).envelope_value(), 0.0) self.assertEqual((signal1 * signal2).envelope_value(3.0), 3.0 * 18.0) self.assertEqual((signal1 * signal2).value(), 0.0) self.assertEqual((signal1 * signal2).value(2.0), 24.0 * np.cos(0.2 * 2.0 * np.pi * 2.0)) # Test piecewise constant dt = 1.0 samples = Array([0.0, 0.0, 1.0, 2.0, 1.0, 0.0, 0.0]) carrier_freq = 0.5 pwc1 = PiecewiseConstant(dt=dt, samples=samples, carrier_freq=carrier_freq) dt = 2.0 samples = Array([0.0, 0.0, 1.0, 2.0, 1.0, 0.0, 0.0]) carrier_freq = 0.1 pwc2 = PiecewiseConstant(dt=dt, samples=samples, carrier_freq=carrier_freq) # Test types self.assertTrue(isinstance(const1 * pwc1, Signal)) self.assertTrue(isinstance(signal1 * pwc1, Signal)) self.assertTrue(isinstance(pwc1 * pwc2, Signal)) self.assertTrue(isinstance(pwc1 * const1, Signal)) self.assertTrue(isinstance(pwc1 * signal1, Signal)) # Test values self.assertEqual((pwc1 * pwc2).carrier_freq, 0.6) self.assertEqual((pwc1 * pwc2).envelope_value(), 0.0) self.assertEqual((pwc1 * pwc2).envelope_value(4.0), 1.0) self.assertEqual((pwc1 * pwc2).value(), 0.0) self.assertEqual((pwc1 * pwc2).value(4.0), 1.0 * np.cos(0.6 * 2.0 * np.pi * 4.0)) # Test phase pwc2 = PiecewiseConstant(dt=dt, samples=samples, carrier_freq=carrier_freq, phase=0.5) self.assertEqual((pwc1 * pwc2).value(4.0), 1.0 * np.cos(0.6 * 2.0 * np.pi * 4.0 + 0.5))
def test_cutoff_freq(self): """Test evaluation with a cutoff frequency.""" # enter frame of drift self.basic_hamiltonian.frame = self.basic_hamiltonian.drift # set cutoff freq to 2 * drive freq (standard RWA) self.basic_hamiltonian.cutoff_freq = 2 * self.w # result should just be the X term halved eval_rwa = self.basic_hamiltonian.evaluate(2.0) expected = 2 * np.pi * (self.r / 2) * self.X.data / 2 self.assertAllClose(eval_rwa, expected) def drive_func(t): return t ** 2 + t ** 3 * 1j self.basic_hamiltonian.signals = [Constant(self.w), Signal(drive_func, self.w)] # result should now contain both X and Y terms halved t = 2.1231 * np.pi dRe = np.real(drive_func(t)) dIm = np.imag(drive_func(t)) eval_rwa = self.basic_hamiltonian.evaluate(t) expected = ( 2 * np.pi * (self.r / 2) * dRe * self.X.data / 2 + 2 * np.pi * (self.r / 2) * dIm * self.Y.data / 2 ) self.assertAllClose(eval_rwa, expected)
def setUp(self): self.t_span = [0.0, 1.0] self.y0 = Array(np.eye(2, dtype=complex)) self.X = Array([[0.0, 1.0], [1.0, 0.0]], dtype=complex) self.Y = Array([[0.0, -1j], [1j, 0.0]], dtype=complex) self.Z = Array([[1.0, 0.0], [0.0, -1.0]], dtype=complex) # simple generator and rhs # pylint: disable=unused-argument def generator(t): return -1j * 2 * np.pi * self.X / 2 def rhs(t, y): return generator(t) @ y self.basic_generator = generator self.basic_rhs = rhs # define simple model self.w = 2.0 self.r = 0.1 signals = [Constant(self.w), Signal(lambda t: 1.0, self.w)] operators = [ -1j * 2 * np.pi * self.Z / 2, -1j * 2 * np.pi * self.r * self.X / 2 ] self.basic_model = GeneratorModel(operators=operators, signals=signals)
def setUp(self): self.X = Array(Operator.from_label("X").data) self.Y = Array(Operator.from_label("Y").data) self.Z = Array(Operator.from_label("Z").data) # define a basic hamiltonian w = 2.0 r = 0.5 operators = [2 * np.pi * self.Z / 2, 2 * np.pi * r * self.X / 2] signals = [Constant(w), Signal(1.0, w)] self.w = w self.r = r self.basic_hamiltonian = HamiltonianModel(operators=operators, signals=signals)
def setUp(self): self.X = Array(Operator.from_label("X").data) self.Y = Array(Operator.from_label("Y").data) self.Z = Array(Operator.from_label("Z").data) # define a basic model w = 2.0 r = 0.5 operators = [ -1j * 2 * np.pi * self.Z / 2, -1j * 2 * np.pi * r * self.X / 2 ] signals = [Constant(w), Signal(1.0, w)] self.w = 2 self.r = r self.basic_model = GeneratorModel(operators=operators, signals=signals)
def setUp(self): self.X = Array([[0.0, 1.0], [1.0, 0.0]], dtype=complex) self.Y = Array([[0.0, -1j], [1j, 0.0]], dtype=complex) self.Z = Array([[1.0, 0.0], [0.0, -1.0]], dtype=complex) # define a basic model w = 2.0 r = 0.5 operators = [-1j * 2 * np.pi * self.Z / 2, -1j * 2 * np.pi * r * self.X / 2] signals = [Constant(w), Signal(1.0, w)] self.w = 2 self.r = r self.basic_model = GeneratorModel(operators=operators, signals=signals) self.y0 = Array([1.0, 0.0], dtype=complex)
def param_sim(amp, drive_freq): signals = [ Constant(1.0), Signal(lambda t: amp, carrier_freq=drive_freq) ] ham_copy = ham.copy() ham_copy.signals = signals results = solve_lmde( ham_copy, t_span=[0.0, 1 / self.r], y0=Array([0.0, 1.0], dtype=complex), method="jax_odeint", atol=1e-10, rtol=1e-10, ) return results.y[-1]
def setUp(self): self.X = Array(Operator.from_label("X").data) self.Y = Array(Operator.from_label("Y").data) self.Z = Array(Operator.from_label("Z").data) # define a basic hamiltonian w = 2.0 r = 0.5 ham_operators = [2 * np.pi * self.Z / 2, 2 * np.pi * r * self.X / 2] ham_signals = [Constant(w), Signal(1.0, w)] self.w = w self.r = r noise_operators = Array([[[0.0, 0.0], [1.0, 0.0]]]) self.basic_lindblad = LindbladModel( hamiltonian_operators=ham_operators, hamiltonian_signals=ham_signals, noise_operators=noise_operators, )