Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
    def testPlot(self):
        """
        Test for plotting functions
        """
        try:
            import matplotlib.pyplot as plt
        except Exception:
            return True
        # step_func
        tlist = np.linspace(0., 2*np.pi, 20)
        processor = Processor(N=1, spline_kind="step_func")
        processor.add_control(sigmaz())
        processor.pulses[0].tlist = tlist
        processor.pulses[0].coeff = np.array([np.sin(t) for t in tlist])
        processor.plot_pulses()
        plt.clf()

        # cubic spline
        tlist = np.linspace(0., 2*np.pi, 20)
        processor = Processor(N=1, spline_kind="cubic")
        processor.add_control(sigmaz())
        processor.pulses[0].tlist = tlist
        processor.pulses[0].coeff = np.array([np.sin(t) for t in tlist])
        processor.plot_pulses()
        plt.clf()
Beispiel #4
0
    def test_plot(self):
        """
        Test for plotting functions
        """
        try:
            import matplotlib.pyplot as plt
        except Exception:
            return True
        # step_func
        tlist = np.linspace(0., 2 * np.pi, 20)
        processor = Processor(N=1, spline_kind="step_func")
        processor.add_control(sigmaz(), label="sz")
        processor.set_all_coeffs({"sz": np.array([np.sin(t) for t in tlist])})
        processor.set_all_tlist(tlist)
        fig, _ = processor.plot_pulses(use_control_latex=False)
        # testing under Xvfb with pytest-xvfb complains if figure windows are
        # left open, so we politely close it:
        plt.close(fig)

        # cubic spline
        tlist = np.linspace(0., 2 * np.pi, 20)
        processor = Processor(N=1, spline_kind="cubic")
        processor.add_control(sigmaz(), label="sz")
        processor.set_all_coeffs({"sz": np.array([np.sin(t) for t in tlist])})
        processor.set_all_tlist(tlist)
        fig, _ = processor.plot_pulses(use_control_latex=False)
        # testing under Xvfb with pytest-xvfb complains if figure windows are
        # left open, so we politely close it:
        plt.close(fig)
Beispiel #5
0
 def testMultiLevelSystem(self):
     """
     Test for processor with multi-level system
     """
     N = 2
     proc = Processor(N=N, dims=[2, 3])
     proc.add_control(tensor(sigmaz(), rand_dm(3, density=1.)), label="sz0")
     proc.set_all_coeffs({"sz0": np.array([1, 2])})
     proc.set_all_tlist(np.array([0., 1., 2]))
     proc.run_state(init_state=tensor([basis(2, 0), basis(3, 1)]))
Beispiel #6
0
 def testMultiLevelSystem(self):
     """
     Test for processor with multi-level system
     """
     N = 2
     proc = Processor(N=N, dims=[2, 3])
     proc.add_control(tensor(sigmaz(), rand_dm(3, density=1.)))
     proc.pulses[0].coeff = np.array([1, 2])
     proc.pulses[0].tlist = np.array([0., 1., 2])
     proc.run_state(init_state=tensor([basis(2, 0), basis(3, 1)]))
Beispiel #7
0
    def test_pulse_mode(self):
        processor = Processor(2)
        processor.add_control(sigmax(), targets=0, label="sx")
        processor.set_coeffs({"sx": np.array([1., 2., 3.])})
        processor.set_tlist({"sx": np.array([0., 1., 2., 3.])})

        processor.pulse_mode = "continuous"
        assert (processor.pulse_mode == "continuous")
        assert (processor.pulses[0].spline_kind == "cubic")
        processor.pulse_mode = "discrete"
        assert (processor.pulse_mode == "discrete")
        assert (processor.pulses[0].spline_kind == "step_func")
Beispiel #8
0
    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)
Beispiel #9
0
 def testChooseSolver(self):
     # 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)
     proc.pulses[0].tlist = tlist
     proc.pulses[0].coeff = np.array([1])
     result = proc.run_state(init_state=init_state, solver="mcsolve")
     assert_allclose(
         fidelity(result.states[-1], qubit_states(2, [0, 1, 0, 0])),
         1, rtol=1.e-7)
Beispiel #10
0
 def testChooseSolver(self):
     # setup and fidelity without noise
     init_state = qubit_states(2, [0, 0, 0, 0])
     tlist = np.linspace(0., np.pi / 2., 10)
     a = destroy(2)
     proc = Processor(N=2, t2=100)
     proc.add_control(sigmax(), targets=1, label="sx")
     proc.set_all_coeffs({"sx": np.array([1.] * len(tlist))})
     proc.set_all_tlist(tlist)
     observerable = tensor([qutip.qeye(2), qutip.sigmax()])
     result1 = proc.run_state(init_state=init_state,
                              solver="mcsolve",
                              e_ops=observerable)
     assert result1.solver == "mcsolve"
Beispiel #11
0
    def test_control_and_coeffs(self):
        processor = Processor(2)
        processor.add_control(sigmax())
        processor.add_control(sigmaz())

        # Set coeffs and tlist without a label
        coeffs = np.array([[1., 2., 3.], [3., 2., 1.]])
        processor.set_coeffs(coeffs)
        assert_allclose(coeffs, processor.coeffs)
        tlist = np.array([0., 1., 2., 3.])
        processor.set_tlist(tlist)
        assert_allclose(tlist, processor.get_full_tlist())
        processor.set_tlist({0: tlist, 1: tlist})
        assert_allclose(tlist, processor.get_full_tlist())

        # Pulses
        assert (len(processor.pulses) == 2)
        assert (processor.find_pulse(0) == processor.pulses[0])
        assert (processor.find_pulse(1) == processor.pulses[1])
        with pytest.raises(KeyError):
            processor.find_pulse("non_exist_pulse")
Beispiel #12
0
    def test_save_read(self):
        """
        Test for saving and reading a pulse matrix
        """
        proc = Processor(N=2)
        proc.add_control(sigmaz(), label="sz")
        proc.add_control(sigmax(), label="sx")
        proc1 = Processor(N=2)
        proc1.add_control(sigmaz(), label="sz")
        proc1.add_control(sigmax(), label="sx")
        proc2 = Processor(N=2)
        proc2.add_control(sigmaz(), label="sz")
        proc2.add_control(sigmax(), label="sx")
        # TODO generalize to different tlist
        tlist = np.array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5])
        amp1 = np.arange(0, 5, 1)
        amp2 = np.arange(5, 0, -1)

        proc.set_all_coeffs({
            label: amp
            for label, amp in zip(proc.get_control_labels(), [amp1, amp2])
        })
        proc.set_all_tlist(tlist)
        proc.save_coeff("qutip_test_CircuitProcessor.txt")
        proc1.read_coeff("qutip_test_CircuitProcessor.txt")
        os.remove("qutip_test_CircuitProcessor.txt")
        assert_allclose(proc1.get_full_coeffs(), proc.get_full_coeffs())
        assert_allclose(proc1.get_full_tlist(), proc.get_full_tlist())
        proc.save_coeff("qutip_test_CircuitProcessor.txt", inctime=False)
        proc2.read_coeff("qutip_test_CircuitProcessor.txt", inctime=False)
        proc2.set_all_tlist(tlist)
        os.remove("qutip_test_CircuitProcessor.txt")
        assert_allclose(proc2.get_full_coeffs(), proc.get_full_coeffs())
Beispiel #13
0
    def test_save_read(self):
        """
        Test for saving and reading a pulse matrix
        """
        proc = Processor(N=2)
        proc.add_control(sigmaz(), cyclic_permutation=True)
        proc1 = Processor(N=2)
        proc1.add_control(sigmaz(), cyclic_permutation=True)
        proc2 = Processor(N=2)
        proc2.add_control(sigmaz(), cyclic_permutation=True)
        # TODO generalize to different tlist
        tlist = np.array([0., 0.1, 0.2, 0.3, 0.4, 0.5])
        amp1 = np.arange(0, 5, 1)
        amp2 = np.arange(5, 0, -1)

        proc.pulses[0].tlist = tlist
        proc.pulses[0].coeff = amp1
        proc.pulses[1].tlist = tlist
        proc.pulses[1].coeff = amp2
        proc.save_coeff("qutip_test_CircuitProcessor.txt")
        proc1.read_coeff("qutip_test_CircuitProcessor.txt")
        os.remove("qutip_test_CircuitProcessor.txt")
        assert_allclose(proc1.get_full_coeffs(), proc.get_full_coeffs())
        assert_allclose(proc1.get_full_tlist(), proc.get_full_tlist())
        proc.save_coeff("qutip_test_CircuitProcessor.txt", inctime=False)
        proc2.read_coeff("qutip_test_CircuitProcessor.txt", inctime=False)
        proc2.set_all_tlist(tlist)
        os.remove("qutip_test_CircuitProcessor.txt")
        assert_allclose(proc2.get_full_coeffs(), proc.get_full_coeffs())
Beispiel #14
0
    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"])
Beispiel #15
0
 def test_modify_ctrls(self):
     """
     Test for modifying Hamiltonian, add_control, remove_pulse
     """
     N = 2
     proc = Processor(N=N)
     proc.ctrls
     proc.add_control(sigmaz())
     assert_(tensor([sigmaz(), identity(2)]), proc.ctrls[0])
     proc.add_control(sigmax(), cyclic_permutation=True)
     assert_allclose(len(proc.ctrls), 3)
     assert_allclose(tensor([sigmax(), identity(2)]), proc.ctrls[1])
     assert_allclose(tensor([identity(2), sigmax()]), proc.ctrls[2])
     proc.add_control(sigmay(), targets=1)
     assert_allclose(tensor([identity(2), sigmay()]), proc.ctrls[3])
     proc.remove_pulse([0, 1, 2])
     assert_allclose(tensor([identity(2), sigmay()]), proc.ctrls[0])
     proc.remove_pulse(0)
     assert_allclose(len(proc.ctrls), 0)
Beispiel #16
0
 def test_pulse_dict(self):
     processor = Processor(1)
     processor.add_control(sigmax(), 0, label="test")
     assert("test" in processor.get_pulse_dict())