def testNoise(self): """ Test for Processor with noise """ # setup and fidelity without noise init_state = qubit_states(2, [0, 0, 0, 0]) tlist = np.array([0., np.pi / 2.]) a = destroy(2) proc = Processor(N=2) proc.add_control(sigmax(), targets=1, label="sx") proc.set_all_coeffs({"sx": np.array([1.])}) proc.set_all_tlist(tlist) result = proc.run_state(init_state=init_state) assert_allclose(fidelity(result.states[-1], qubit_states(2, [0, 1, 0, 0])), 1, rtol=1.e-7) # decoherence noise dec_noise = DecoherenceNoise([0.25 * a], targets=1) proc.add_noise(dec_noise) result = proc.run_state(init_state=init_state) assert_allclose(fidelity(result.states[-1], qubit_states(2, [0, 1, 0, 0])), 0.981852, rtol=1.e-3) # white random noise proc.model._noise = [] white_noise = RandomNoise(0.2, np.random.normal, loc=0.1, scale=0.1) proc.add_noise(white_noise) result = proc.run_state(init_state=init_state)
def testSpline(self): """ Test if the spline kind is correctly transfered into the arguments in QobjEvo """ tlist = np.array([1, 2, 3, 4, 5, 6], dtype=float) coeff = np.array([1, 1, 1, 1, 1, 1], dtype=float) processor = Processor(N=1, spline_kind="step_func") processor.add_control(sigmaz(), label="sz") processor.set_all_coeffs({"sz": coeff}) processor.set_tlist(tlist) ideal_qobjevo, _ = processor.get_qobjevo(noisy=False) assert_(ideal_qobjevo.args["_step_func_coeff"]) noisy_qobjevo, c_ops = processor.get_qobjevo(noisy=True) assert_(noisy_qobjevo.args["_step_func_coeff"]) processor.add_noise(ControlAmpNoise(coeff=coeff, tlist=tlist)) noisy_qobjevo, c_ops = processor.get_qobjevo(noisy=True) assert_(noisy_qobjevo.args["_step_func_coeff"]) tlist = np.array([1, 2, 3, 4, 5, 6], dtype=float) coeff = np.array([1, 1, 1, 1, 1, 1], dtype=float) processor = Processor(N=1, spline_kind="cubic") processor.add_control(sigmaz(), label="sz") processor.set_all_coeffs({"sz": coeff}) processor.set_all_tlist(tlist) ideal_qobjevo, _ = processor.get_qobjevo(noisy=False) assert ("_step_func_coeff" not in ideal_qobjevo.args) noisy_qobjevo, c_ops = processor.get_qobjevo(noisy=True) assert ("_step_func_coeff" not in noisy_qobjevo.args) processor.t1 = 100. processor.add_noise(ControlAmpNoise(coeff=coeff, tlist=tlist)) noisy_qobjevo, c_ops = processor.get_qobjevo(noisy=True) assert ("_step_func_coeff" not in noisy_qobjevo.args)
def testGetObjevo(self): tlist = np.array([1, 2, 3, 4, 5, 6], dtype=float) coeff = np.array([1, 1, 1, 1, 1, 1], dtype=float) processor = Processor(N=1) processor.add_control(sigmaz()) processor.pulses[0].tlist = tlist processor.pulses[0].coeff = coeff # without noise unitary_qobjevo, _ = processor.get_qobjevo( args={"test": True}, noisy=False) assert_allclose(unitary_qobjevo.ops[0].qobj, sigmaz()) assert_allclose(unitary_qobjevo.tlist, tlist) assert_allclose(unitary_qobjevo.ops[0].coeff, coeff[0]) assert_(unitary_qobjevo.args["test"], msg="Arguments not correctly passed on") # with decoherence noise dec_noise = DecoherenceNoise( c_ops=sigmax(), coeff=coeff, tlist=tlist) processor.add_noise(dec_noise) assert_equal(unitary_qobjevo.to_list(), processor.get_qobjevo(noisy=False)[0].to_list()) noisy_qobjevo, c_ops = processor.get_qobjevo( args={"test": True}, noisy=True) assert_(noisy_qobjevo.args["_step_func_coeff"], msg="Spline type not correctly passed on") assert_(noisy_qobjevo.args["test"], msg="Arguments not correctly passed on") assert_(sigmaz() in [pair[0] for pair in noisy_qobjevo.to_list()]) assert_equal(c_ops[0].ops[0].qobj, sigmax()) assert_equal(c_ops[0].tlist, tlist) # with amplitude noise processor = Processor(N=1, spline_kind="cubic") processor.add_control(sigmaz()) tlist = np.linspace(1, 6, int(5/0.2)) coeff = np.random.rand(len(tlist)) processor.pulses[0].tlist = tlist processor.pulses[0].coeff = coeff amp_noise = ControlAmpNoise(coeff=coeff, tlist=tlist) processor.add_noise(amp_noise) noisy_qobjevo, c_ops = processor.get_qobjevo( args={"test": True}, noisy=True) assert_(not noisy_qobjevo.args["_step_func_coeff"], msg="Spline type not correctly passed on") assert_(noisy_qobjevo.args["test"], msg="Arguments not correctly passed on") assert_equal(len(noisy_qobjevo.ops), 2) assert_equal(sigmaz(), noisy_qobjevo.ops[0].qobj) assert_allclose(coeff, noisy_qobjevo.ops[0].coeff, rtol=1.e-10)
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 testSpline(self): """ Test if the spline kind is correctly transfered into the arguments in QobjEvo """ tlist = np.array([1, 2, 3, 4, 5, 6], dtype=float) coeff = np.array([1, 1, 1, 1, 1, 1], dtype=float) processor = Processor(N=1, spline_kind="step_func") processor.add_control(sigmaz()) processor.pulses[0].tlist = tlist processor.pulses[0].coeff = coeff ideal_qobjevo, _ = processor.get_qobjevo(noisy=False) assert_(ideal_qobjevo.args["_step_func_coeff"]) noisy_qobjevo, c_ops = processor.get_qobjevo(noisy=True) assert_(noisy_qobjevo.args["_step_func_coeff"]) processor.T1 = 100. processor.add_noise(ControlAmpNoise(coeff=coeff, tlist=tlist)) noisy_qobjevo, c_ops = processor.get_qobjevo(noisy=True) assert_(noisy_qobjevo.args["_step_func_coeff"]) tlist = np.array([1, 2, 3, 4, 5, 6], dtype=float) coeff = np.array([1, 1, 1, 1, 1, 1], dtype=float) processor = Processor(N=1, spline_kind="cubic") processor.add_control(sigmaz()) processor.pulses[0].tlist = tlist processor.pulses[0].coeff = coeff ideal_qobjevo, _ = processor.get_qobjevo(noisy=False) assert_(not ideal_qobjevo.args["_step_func_coeff"]) noisy_qobjevo, c_ops = processor.get_qobjevo(noisy=True) assert_(not noisy_qobjevo.args["_step_func_coeff"]) processor.T1 = 100. processor.add_noise(ControlAmpNoise(coeff=coeff, tlist=tlist)) noisy_qobjevo, c_ops = processor.get_qobjevo(noisy=True) assert_(not noisy_qobjevo.args["_step_func_coeff"])