def test_no_saving_intermidiate_state(self): processor = Processor(1) processor.add_pulse(pulse=Pulse(sigmax(), coeff=np.ones(10), tlist=np.linspace(0, 1, 10), targets=0)) result = processor.run_state(basis(2, 0), tlist=[0, 1]) assert (len(result.states) == 2)
def test_id_with_T1_T2(self): """ Test for identity evolution with relaxation t1 and t2 """ # setup a = destroy(2) Hadamard = hadamard_transform(1) ex_state = basis(2, 1) mines_state = (basis(2, 1) - basis(2, 0)).unit() end_time = 2. tlist = np.arange(0, end_time + 0.02, 0.02) t1 = 1. t2 = 0.5 # test t1 test = Processor(1, t1=t1) # zero ham evolution test.add_pulse(Pulse(identity(2), 0, tlist, False)) result = test.run_state(ex_state, e_ops=[a.dag() * a]) assert_allclose(result.expect[0][-1], np.exp(-1. / t1 * end_time), rtol=1e-5, err_msg="Error in t1 time simulation") # test t2 test = Processor(1, t2=t2) test.add_pulse(Pulse(identity(2), 0, tlist, False)) result = test.run_state(init_state=mines_state, e_ops=[Hadamard * a.dag() * a * Hadamard]) assert_allclose(result.expect[0][-1], np.exp(-1. / t2 * end_time) * 0.5 + 0.5, rtol=1e-5, err_msg="Error in t2 time simulation") # test t1 and t2 t1 = np.random.rand(1) + 0.5 t2 = np.random.rand(1) * 0.5 + 0.5 test = Processor(1, t1=t1, t2=t2) test.add_pulse(Pulse(identity(2), 0, tlist, False)) result = test.run_state(init_state=mines_state, e_ops=[Hadamard * a.dag() * a * Hadamard]) assert_allclose(result.expect[0][-1], np.exp(-1. / t2 * end_time) * 0.5 + 0.5, rtol=1e-5, err_msg="Error in t1 & t2 simulation, " "with t1={} and t2={}".format(t1, t2))
def testDrift(self): """ Test for the drift Hamiltonian """ processor = Processor(N=1) processor.add_drift(sigmaz(), 0) tlist = np.array([0., 1., 2.]) processor.add_pulse(Pulse(identity(2), 0, tlist, False)) ideal_qobjevo, _ = processor.get_qobjevo(noisy=True) assert_equal(ideal_qobjevo.cte, sigmaz())
def test_user_defined_noise(self): """ Test for the user-defined noise object """ dr_noise = DriftNoise(sigmax()) proc = Processor(1) proc.add_noise(dr_noise) tlist = np.array([0, np.pi/2.]) proc.add_pulse(Pulse(identity(2), 0, tlist, False)) result = proc.run_state(init_state=basis(2, 0)) assert_allclose( fidelity(result.states[-1], basis(2, 1)), 1, rtol=1.0e-6)
def test_id_evolution(self): """ Test for identity evolution """ N = 1 proc = Processor(N=N) init_state = rand_ket(2) tlist = [0., 1., 2.] proc.add_pulse(Pulse(identity(2), 0, tlist, False)) result = proc.run_state( init_state, options=Options(store_final_state=True)) global_phase = init_state.data[0, 0]/result.final_state.data[0, 0] assert_allclose(global_phase*result.final_state, init_state)
def TestControlAmpNoise(self): """ Test for the control amplitude noise """ tlist = np.array([1, 2, 3, 4, 5, 6]) coeff = np.array([1, 1, 1, 1, 1, 1]) # use proc_qobjevo pulses = [Pulse(sigmaz(), 0, tlist, coeff)] connoise = ControlAmpNoise(coeff=coeff, tlist=tlist) noisy_pulses = connoise.get_noisy_dynamics(pulses=pulses) assert_allclose(noisy_pulses[0].coherent_noise[0].qobj, sigmaz()) assert_allclose(noisy_pulses[0].coherent_noise[0].coeff, coeff)
def test_relaxation_noise(self): """ Test for the relaxation noise """ # only t1 a = destroy(2) dims = [2] * 3 relnoise = RelaxationNoise(t1=[1., 1., 1.], t2=None) systematic_noise = Pulse(None, None, label="system") pulses, systematic_noise = relnoise.get_noisy_pulses( dims=dims, systematic_noise=systematic_noise) noisy_qu, c_ops = systematic_noise.get_noisy_qobjevo(dims=dims) assert_(len(c_ops) == 3) assert_allclose(c_ops[1](0).full(), tensor([qeye(2), a, qeye(2)]).full()) # no relaxation dims = [2] * 2 relnoise = RelaxationNoise(t1=None, t2=None) systematic_noise = Pulse(None, None, label="system") pulses, systematic_noise = relnoise.get_noisy_pulses( dims=dims, systematic_noise=systematic_noise) noisy_qu, c_ops = systematic_noise.get_noisy_qobjevo(dims=dims) assert_(len(c_ops) == 0) # only t2 relnoise = RelaxationNoise(t1=None, t2=[0.2, 0.7]) systematic_noise = Pulse(None, None, label="system") pulses, systematic_noise = relnoise.get_noisy_pulses( dims=dims, systematic_noise=systematic_noise) noisy_qu, c_ops = systematic_noise.get_noisy_qobjevo(dims=dims) assert_(len(c_ops) == 2) # t1+t2 and systematic_noise = None relnoise = RelaxationNoise(t1=[1., 1.], t2=[0.5, 0.5]) pulses, systematic_noise = relnoise.get_noisy_pulses(dims=dims) noisy_qu, c_ops = systematic_noise.get_noisy_qobjevo(dims=dims) assert_(len(c_ops) == 4)
def test_control_amplitude_noise(self): """ Test for the control amplitude noise """ tlist = np.array([1, 2, 3, 4, 5, 6]) coeff = np.array([1, 1, 1, 1, 1, 1]) # use proc_qobjevo pulses = [Pulse(sigmaz(), 0, tlist, coeff)] connoise = ControlAmpNoise(coeff=coeff, tlist=tlist) noisy_pulses, systematic_noise = \ connoise.get_noisy_pulses(pulses=pulses) assert_allclose(pulses[0].coherent_noise[0].qobj.full(), sigmaz().full()) assert_allclose(noisy_pulses[0].coherent_noise[0].coeff, coeff)
def testDrift(self): """ Test for the drift Hamiltonian """ processor = Processor(N=1) processor.add_drift(sigmax() / 2, 0) tlist = np.array([0., np.pi, 2 * np.pi, 3 * np.pi]) processor.add_pulse(Pulse(None, None, tlist, False)) ideal_qobjevo, _ = processor.get_qobjevo(noisy=True) assert_equal(ideal_qobjevo(0), sigmax() / 2) init_state = basis(2) propagators = processor.run_analytically() analytical_result = init_state for unitary in propagators: analytical_result = unitary * analytical_result fid = fidelity(sigmax() * init_state, analytical_result) assert ((1 - fid) < 1.0e-6)
def test_coherent_noise(self): """ Test for pulse genration with coherent noise. """ coeff = np.array([0.1, 0.2, 0.3, 0.4]) tlist = np.array([0., 1., 2., 3.]) ham = sigmaz() pulse1 = Pulse(ham, 1, tlist, coeff) # Add coherent noise with the same tlist pulse1.add_coherent_noise(sigmay(), 0, tlist, coeff) assert_allclose( pulse1.get_ideal_qobjevo(2)(0).full(), tensor(identity(2), sigmaz()).full() * 0.1) assert_(len(pulse1.coherent_noise) == 1) noise_qu, c_ops = pulse1.get_noisy_qobjevo(2) assert_allclose(c_ops, []) assert_allclose(pulse1.get_full_tlist(), np.array([0., 1., 2., 3.]))
def test_decoherence_noise(self): """ Test for the decoherence noise """ tlist = np.array([0, 1, 2, 3, 4, 5, 6]) coeff = np.array([1, 1, 1, 1, 1, 1, 1]) # Time-dependent decnoise = DecoherenceNoise(sigmaz(), coeff=coeff, tlist=tlist, targets=[1]) dims = [2] * 2 systematic_noise = Pulse(None, None, label="systematic_noise", spline_kind="step_func") pulses, systematic_noise = decnoise.get_noisy_pulses( systematic_noise=systematic_noise, dims=dims) noisy_qu, c_ops = systematic_noise.get_noisy_qobjevo(dims=dims) assert_allclose(c_ops[0](0).full(), tensor(qeye(2), sigmaz()).full()) # Time-independent and all qubits decnoise = DecoherenceNoise(sigmax(), all_qubits=True) pulses, systematic_noise = decnoise.get_noisy_pulses(dims=dims) noisy_qu, c_ops = systematic_noise.get_noisy_qobjevo(dims=dims) c_ops = [qu(0) for qu in c_ops] assert_(tensor([qeye(2), sigmax()]) in c_ops) assert_(tensor([sigmax(), qeye(2)]) in c_ops) # Time-denpendent and all qubits decnoise = DecoherenceNoise(sigmax(), all_qubits=True, coeff=coeff * 2, tlist=tlist) systematic_noise = Pulse(None, None, label="systematic_noise", spline_kind="step_func") pulses, systematic_noise = decnoise.get_noisy_pulses( systematic_noise=systematic_noise, dims=dims) noisy_qu, c_ops = systematic_noise.get_noisy_qobjevo(dims=dims) assert_allclose(c_ops[0](0).full(), tensor(sigmax(), qeye(2)).full() * 2) assert_allclose(c_ops[1](0).full(), tensor(qeye(2), sigmax()).full() * 2)
def test_user_defined_noise(noise_class, mode): """ Test for the user-defined noise object """ dr_noise = noise_class(sigmax()) proc = Processor(1) proc.add_noise(dr_noise) tlist = np.array([0, np.pi / 2.]) proc.add_pulse(Pulse(identity(2), 0, tlist, False)) if mode == "warning": with pytest.warns(PendingDeprecationWarning): result = proc.run_state(init_state=basis(2, 0)) elif mode == "error": with pytest.raises(NotImplementedError): result = proc.run_state(init_state=basis(2, 0)) return else: result = proc.run_state(init_state=basis(2, 0)) assert_allclose(fidelity(result.states[-1], basis(2, 1)), 1, rtol=1.0e-6)
def test_random_noise(self): """ Test for the white noise """ tlist = np.array([1, 2, 3, 4, 5, 6]) coeff = np.array([1, 1, 1, 1, 1, 1]) dummy_qobjevo = QobjEvo(sigmaz(), tlist=tlist) mean = 0. std = 0.5 pulses = [ Pulse(sigmaz(), 0, tlist, coeff), Pulse(sigmax(), 0, tlist, coeff * 2), Pulse(sigmay(), 0, tlist, coeff * 3) ] # random noise with operators from proc_qobjevo gaussnoise = RandomNoise(dt=0.1, rand_gen=np.random.normal, loc=mean, scale=std) noisy_pulses, systematic_noise = \ gaussnoise.get_noisy_pulses(pulses=pulses) assert_allclose(noisy_pulses[2].qobj.full(), sigmay().full()) assert_allclose(noisy_pulses[1].coherent_noise[0].qobj.full(), sigmax().full()) assert_allclose(len(noisy_pulses[0].coherent_noise[0].tlist), len(noisy_pulses[0].coherent_noise[0].coeff)) # random noise with dt and other random number generator pulses = [ Pulse(sigmaz(), 0, tlist, coeff), Pulse(sigmax(), 0, tlist, coeff * 2), Pulse(sigmay(), 0, tlist, coeff * 3) ] gaussnoise = RandomNoise(lam=0.1, dt=0.2, rand_gen=np.random.poisson) assert_(gaussnoise.rand_gen is np.random.poisson) noisy_pulses, systematic_noise = \ gaussnoise.get_noisy_pulses(pulses=pulses) assert_allclose(noisy_pulses[0].coherent_noise[0].tlist, np.linspace(1, 6, int(5 / 0.2) + 1)) assert_allclose(noisy_pulses[1].coherent_noise[0].tlist, np.linspace(1, 6, int(5 / 0.2) + 1)) assert_allclose(noisy_pulses[2].coherent_noise[0].tlist, np.linspace(1, 6, int(5 / 0.2) + 1))
def TestCoherentNoise(): """ Test for pulse genration with coherent noise. """ coeff = np.array([0.1, 0.2, 0.3, 0.4]) tlist = np.array([0., 1., 2., 3.]) ham = sigmaz() pulse1 = Pulse(ham, 1, tlist, coeff) # Add coherent noise with the same tlist pulse1.add_coherent_noise(sigmay(), 0, tlist, coeff) assert_allclose( pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) assert_(len(pulse1.coherent_noise) == 1) noise_qu, c_ops = pulse1.get_noisy_qobjevo(2) assert_allclose(c_ops, []) assert_allclose(noise_qu.tlist, np.array([0., 1., 2., 3.])) qobj_list = [ele.qobj for ele in noise_qu.ops] assert_(tensor(identity(2), sigmaz()) in qobj_list) assert_(tensor(sigmay(), identity(2)) in qobj_list) for ele in noise_qu.ops: assert_allclose(ele.coeff, coeff)
def TestNoisyPulse(): """ Test for lindblad noise and different tlist """ coeff = np.array([0.1, 0.2, 0.3, 0.4]) tlist = np.array([0., 1., 2., 3.]) ham = sigmaz() pulse1 = Pulse(ham, 1, tlist, coeff) # Add coherent noise and lindblad noise with different tlist pulse1.spline_kind = "step_func" tlist_noise = np.array([1., 2.5, 3.]) coeff_noise = np.array([0.5, 0.1, 0.5]) pulse1.add_coherent_noise(sigmay(), 0, tlist_noise, coeff_noise) tlist_noise2 = np.array([0.5, 2, 3.]) coeff_noise2 = np.array([0.1, 0.2, 0.3]) pulse1.add_lindblad_noise(sigmax(), 1, coeff=True) pulse1.add_lindblad_noise( sigmax(), 0, tlist=tlist_noise2, coeff=coeff_noise2) assert_allclose( pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) noise_qu, c_ops = pulse1.get_noisy_qobjevo(2) assert_allclose(noise_qu.tlist, np.array([0., 0.5, 1., 2., 2.5, 3.])) for ele in noise_qu.ops: if ele.qobj == tensor(identity(2), sigmaz()): assert_allclose( ele.coeff, np.array([0.1, 0.1, 0.2, 0.3, 0.3, 0.4])) elif ele.qobj == tensor(sigmay(), identity(2)): assert_allclose( ele.coeff, np.array([0., 0., 0.5, 0.5, 0.1, 0.5])) for c_op in c_ops: if len(c_op.ops) == 0: assert_allclose(c_ops[0].cte, tensor(identity(2), sigmax())) else: assert_allclose( c_ops[1].ops[0].qobj, tensor(sigmax(), identity(2))) assert_allclose( c_ops[1].tlist, np.array([0., 0.5, 1., 2., 2.5, 3.])) assert_allclose( c_ops[1].ops[0].coeff, np.array([0., 0.1, 0.1, 0.2, 0.2, 0.3]))
def test_repeated_use_of_processor(self): processor = Processor(1, t1=1.) processor.add_pulse(Pulse(sigmax(), targets=0, coeff=True)) result1 = processor.run_state(basis(2, 0), tlist=np.linspace(0, 1, 10)) result2 = processor.run_state(basis(2, 0), tlist=np.linspace(0, 1, 10)) assert_allclose(result1.states[-1].full(), result2.states[-1].full())
def TestPulseConstructor(): """ Test for creating empty Pulse, Pulse with constant coefficients etc. """ coeff = np.array([0.1, 0.2, 0.3, 0.4]) tlist = np.array([0., 1., 2., 3.]) ham = sigmaz() # Special ways of initializing pulse pulse2 = Pulse(sigmax(), 0, tlist, True) assert_allclose(pulse2.get_ideal_qobjevo(2).ops[0].qobj, tensor(sigmax(), identity(2))) pulse3 = Pulse(sigmay(), 0) assert_allclose(pulse3.get_ideal_qobjevo(2).cte.norm(), 0.) pulse4 = Pulse(None, None) # Dummy empty ham assert_allclose(pulse4.get_ideal_qobjevo(2).cte.norm(), 0.) tlist_noise = np.array([1., 2.5, 3.]) coeff_noise = np.array([0.5, 0.1, 0.5]) tlist_noise2 = np.array([0.5, 2, 3.]) coeff_noise2 = np.array([0.1, 0.2, 0.3]) # Pulse with different dims random_qobj = Qobj(np.random.random((3, 3))) pulse5 = Pulse(sigmaz(), 1, tlist, True) pulse5.add_coherent_noise(sigmay(), 1, tlist_noise, coeff_noise) pulse5.add_lindblad_noise( random_qobj, 0, tlist=tlist_noise2, coeff=coeff_noise2) qu, c_ops = pulse5.get_noisy_qobjevo(dims=[3, 2]) assert_allclose(qu.ops[0].qobj, tensor([identity(3), sigmaz()])) assert_allclose(qu.ops[1].qobj, tensor([identity(3), sigmay()])) assert_allclose(c_ops[0].ops[0].qobj, tensor([random_qobj, identity(2)]))
def TestBasicPulse(): """ Test for basic pulse generation and attributes. """ coeff = np.array([0.1, 0.2, 0.3, 0.4]) tlist = np.array([0., 1., 2., 3.]) ham = sigmaz() # Basic tests pulse1 = Pulse(ham, 1, tlist, coeff) assert_allclose( pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) pulse1.tlist = 2 * tlist assert_allclose(pulse1.tlist, 2 * tlist) pulse1.tlist = tlist pulse1.coeff = 2 * coeff assert_allclose(pulse1.coeff, 2 * coeff) pulse1.coeff = coeff pulse1.qobj = 2 * sigmay() assert_allclose(pulse1.qobj, 2 * sigmay()) pulse1.qobj = ham pulse1.targets = 3 assert_allclose(pulse1.targets, 3) pulse1.targets = 1 assert_allclose(pulse1.get_ideal_qobj(2), tensor(identity(2), sigmaz()))
def test_noisy_pulse(self): """ Test for lindblad noise and different tlist """ coeff = np.array([0.1, 0.2, 0.3, 0.4]) tlist = np.array([0., 1., 2., 3.]) ham = sigmaz() pulse1 = Pulse(ham, 1, tlist, coeff) # Add coherent noise and lindblad noise with different tlist pulse1.spline_kind = "step_func" tlist_noise = np.array([0., 1., 2.5, 3.]) coeff_noise = np.array([0., 0.5, 0.1, 0.5]) pulse1.add_coherent_noise(sigmay(), 0, tlist_noise, coeff_noise) tlist_noise2 = np.array([0., 0.5, 2, 3.]) coeff_noise2 = np.array([0., 0.1, 0.2, 0.3]) pulse1.add_lindblad_noise(sigmax(), 1, coeff=True) pulse1.add_lindblad_noise(sigmax(), 0, tlist=tlist_noise2, coeff=coeff_noise2) assert_allclose( pulse1.get_ideal_qobjevo(2)(0).full(), tensor(identity(2), sigmaz()).full() * 0.1) noise_qu, c_ops = pulse1.get_noisy_qobjevo(2) assert_allclose(pulse1.get_full_tlist(), np.array([0., 0.5, 1., 2., 2.5, 3.])) if parse_version(qutip.__version__) >= parse_version('5.dev'): expected = QobjEvo([[ tensor(identity(2), sigmaz()), np.array([0.1, 0.1, 0.2, 0.3, 0.3, 0.4]) ], [ tensor(sigmay(), identity(2)), np.array([0., 0., 0.5, 0.5, 0.1, 0.5]) ]], tlist=np.array([0., 0.5, 1., 2., 2.5, 3.]), order=0) else: expected = QobjEvo([[ tensor(identity(2), sigmaz()), np.array([0.1, 0.1, 0.2, 0.3, 0.3, 0.4]) ], [ tensor(sigmay(), identity(2)), np.array([0., 0., 0.5, 0.5, 0.1, 0.5]) ]], tlist=np.array([0., 0.5, 1., 2., 2.5, 3.]), args={"_step_func_coeff": True}) _compare_qobjevo(noise_qu, expected, 0, 3) for c_op in c_ops: try: isconstant = c_op.isconstant except AttributeError: isconstant = (len(c_op.ops) == 0) if isconstant: assert_allclose( c_op(0).full(), tensor(identity(2), sigmax()).full()) else: if parse_version(qutip.__version__) >= parse_version('5.dev'): expected = QobjEvo([ tensor(sigmax(), identity(2)), np.array([0., 0.1, 0.1, 0.2, 0.2, 0.3]) ], tlist=np.array( [0., 0.5, 1., 2., 2.5, 3.]), order=0) else: expected = QobjEvo([ tensor(sigmax(), identity(2)), np.array([0., 0.1, 0.1, 0.2, 0.2, 0.3]) ], tlist=np.array( [0., 0.5, 1., 2., 2.5, 3.]), args={"_step_func_coeff": True}) _compare_qobjevo(c_op, expected, 0, 3)
def test_basic_pulse(self): """ Test for basic pulse generation and attributes. """ coeff = np.array([0.1, 0.2, 0.3, 0.4]) tlist = np.array([0., 1., 2., 3.]) ham = sigmaz() # Basic tests pulse1 = Pulse(ham, 1, tlist, coeff) assert_allclose( pulse1.get_ideal_qobjevo(2)(0).full(), tensor(identity(2), sigmaz()).full() * coeff[0]) pulse1.tlist = 2 * tlist assert_allclose(pulse1.tlist, 2 * tlist) pulse1.tlist = tlist pulse1.coeff = 2 * coeff assert_allclose(pulse1.coeff, 2 * coeff) pulse1.coeff = coeff pulse1.qobj = 2 * sigmay() assert_allclose(pulse1.qobj.full(), 2 * sigmay().full()) pulse1.qobj = ham pulse1.targets = 3 assert_allclose(pulse1.targets, 3) pulse1.targets = 1 qobjevo = pulse1.get_ideal_qobjevo(2) if parse_version(qutip.__version__) >= parse_version('5.dev'): expected = QobjEvo([tensor(identity(2), sigmaz()), coeff], tlist=tlist, order=0) else: expected = QobjEvo([tensor(identity(2), sigmaz()), coeff], tlist=tlist, args={"_step_func_coeff": True}) _compare_qobjevo(qobjevo, expected, 0, 3)
def test_pulse_constructor(self): """ Test for creating empty Pulse, Pulse with constant coefficients etc. """ coeff = np.array([0.1, 0.2, 0.3, 0.4]) tlist = np.array([0., 1., 2., 3.]) ham = sigmaz() # Special ways of initializing pulse pulse2 = Pulse(sigmax(), 0, tlist, True) assert_allclose( pulse2.get_ideal_qobjevo(2)(0).full(), tensor(sigmax(), identity(2)).full()) pulse3 = Pulse(sigmay(), 0) assert_allclose(pulse3.get_ideal_qobjevo(2)(0).norm(), 0.) pulse4 = Pulse(None, None) # Dummy empty ham assert_allclose(pulse4.get_ideal_qobjevo(2)(0).norm(), 0.) tlist_noise = np.array([1., 2.5, 3.]) coeff_noise = np.array([0.5, 0.1, 0.5]) tlist_noise2 = np.array([0.5, 2, 3.]) coeff_noise2 = np.array([0.1, 0.2, 0.3]) # Pulse with different dims random_qobj = Qobj(np.random.random((3, 3))) pulse5 = Pulse(sigmaz(), 1, tlist, True) pulse5.add_coherent_noise(sigmay(), 1, tlist_noise, coeff_noise) pulse5.add_lindblad_noise(random_qobj, 0, tlist=tlist_noise2, coeff=coeff_noise2) qu, c_ops = pulse5.get_noisy_qobjevo(dims=[3, 2]) if parse_version(qutip.__version__) >= parse_version('5.dev'): expected = QobjEvo([ tensor([identity(3), sigmaz()]), [tensor([identity(3), sigmay()]), coeff_noise] ], tlist=tlist_noise, order=0) else: expected = QobjEvo([ tensor([identity(3), sigmaz()]), [tensor([identity(3), sigmay()]), coeff_noise] ], tlist=tlist_noise, args={"_step_func_coeff": True}) _compare_qobjevo(qu, expected, 0, 3)
def get_noisy_dynamics(self, ctrl_pulses, dims=None): dummy = Pulse(None, None) dummy.add_coherent_noise(self.qobj, 0, coeff=True) return ctrl_pulses + [dummy]