예제 #1
0
 def TestMultiLevelSystem(self):
     """
     Test for processor with multi-level system
     """
     N = 2
     proc = Processor(N=N, dims=[2, 3])
     proc.add_ctrl(tensor(sigmaz(), rand_dm(3, density=1.)))
     proc.coeffs = np.array([1, 2]).reshape((1, 2))
     proc.tlist = np.array([0., 1., 2])
     proc.run_state(rho0=tensor([basis(2, 0), basis(3, 1)]))
예제 #2
0
    def TestGetObjevo(self):
        tlist = np.array([1, 2, 3, 4, 5, 6], dtype=float)
        coeffs = np.array([[1, 1, 1, 1, 1, 1]], dtype=float)
        processor = Processor(N=1)
        processor.add_ctrl(sigmaz())
        processor.tlist = tlist
        processor.coeffs = coeffs

        # without noise
        unitary_qobjevo = processor.get_unitary_qobjevo(args={"test": True})
        assert_allclose(unitary_qobjevo.ops[0].qobj, sigmaz())
        assert_allclose(unitary_qobjevo.tlist, tlist)
        assert_allclose(unitary_qobjevo.ops[0].coeff, coeffs[0])
        assert_(unitary_qobjevo.args["test"],
                msg="Arguments not correctly passed on")

        # with decoherence noise
        dec_noise = DecoherenceNoise(c_ops=sigmax(),
                                     coeffs=coeffs,
                                     tlist=tlist)
        processor.add_noise(dec_noise)
        assert_equal(unitary_qobjevo.to_list(),
                     processor.get_unitary_qobjevo().to_list())

        noisy_qobjevo, c_ops = processor.get_noisy_qobjevo(args={"test": 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.spline_kind = "cubic"
        new_tlist = np.linspace(1, 6, int(5 / 0.2))
        new_coeffs = np.random.rand(1, len(new_tlist))
        # noise with a different operator
        amp_noise = ControlAmpNoise(ops=sigmax(), coeffs=coeffs, tlist=tlist)
        processor.add_noise(amp_noise)
        noisy_qobjevo, c_ops = processor.get_noisy_qobjevo(args={"test": True})
        assert_(not noisy_qobjevo.args["_step_func_coeff"],
                msg="Spline type not correctly passed on")
        assert_(sigmax() in [pair[0] for pair in noisy_qobjevo.to_list()])
        # noise with operators in the processor
        # Since the noise operator is also sigmaz,
        # it should be merged with the original operator
        amp_noise2 = ControlAmpNoise(coeffs=coeffs, tlist=tlist)
        processor.noise[1] = amp_noise2
        noisy_qobjevo, c_ops = processor.get_noisy_qobjevo(args={"test": True})
        assert_(not noisy_qobjevo.args["_step_func_coeff"],
                msg="Spline type not correctly passed on")
        assert_equal(len(noisy_qobjevo.ops), 1)
        assert_equal(sigmaz(), noisy_qobjevo.ops[0].qobj)
        assert_equal(coeffs[0] * 2, noisy_qobjevo.ops[0].coeff)
예제 #3
0
    def TestSpline(self):
        tlist = np.array([1, 2, 3, 4, 5, 6], dtype=float)
        coeffs = np.array([[1, 1, 1, 1, 1, 1]], dtype=float)
        processor = Processor(N=1)
        processor.add_ctrl(sigmaz())
        processor.tlist = tlist
        processor.coeffs = coeffs

        processor.spline_kind = "step_func"
        noisy_qobjevo, c_ops = processor.get_noisy_qobjevo()
        assert_(noisy_qobjevo.args["_step_func_coeff"])

        processor.spline_kind = "cubic"
        noisy_qobjevo, c_ops = processor.get_noisy_qobjevo()
        assert_(not noisy_qobjevo.args["_step_func_coeff"])
예제 #4
0
 def test_modify_ctrls(self):
     """
     Test for modifying Hamiltonian, add_ctrl, remove_ctrl
     """
     N = 2
     proc = Processor(N=N)
     proc.ctrls
     proc.ctrls = [sigmaz()]
     assert_(tensor([sigmaz(), identity(2)]), proc.ctrls[0])
     proc.add_ctrl(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_ctrl(sigmay(), targets=1)
     assert_allclose(tensor([identity(2), sigmay()]), proc.ctrls[3])
     proc.remove_ctrl([0, 1, 2])
     assert_allclose(tensor([identity(2), sigmay()]), proc.ctrls[0])
     proc.remove_ctrl(0)
     assert_allclose(len(proc.ctrls), 0)
예제 #5
0
    def test_save_read(self):
        """
        Test for saving and reading a pulse matrix
        """
        proc = Processor(N=2)
        proc.add_ctrl(sigmaz(), cyclic_permutation=True)
        proc1 = Processor(N=2)
        proc1.add_ctrl(sigmaz(), cyclic_permutation=True)
        proc2 = Processor(N=2)
        proc2.add_ctrl(sigmaz(), cyclic_permutation=True)
        tlist = [0., 0.1, 0.2, 0.3, 0.4, 0.5]
        amp1 = np.arange(0, 5, 1)
        amp2 = np.arange(5, 0, -1)

        proc.tlist = tlist
        proc.coeffs = np.array([amp1, amp2])
        proc.save_coeff("qutip_test_CircuitProcessor.txt")
        proc1.read_coeff("qutip_test_CircuitProcessor.txt")
        os.remove("qutip_test_CircuitProcessor.txt")
        assert_allclose(proc1.coeffs, proc.coeffs)
        assert_allclose(proc1.tlist, proc.tlist)
        proc.save_coeff("qutip_test_CircuitProcessor.txt", inctime=False)
        proc2.read_coeff("qutip_test_CircuitProcessor.txt", inctime=False)
        os.remove("qutip_test_CircuitProcessor.txt")
        assert_allclose(proc2.coeffs, proc.coeffs)
        assert_(proc2.tlist is None)
예제 #6
0
    def TestPlot(self):
        try:
            import matplotlib.pyplot as plt
        except:
            return True
        # step_func
        tlist = np.linspace(0., 2 * np.pi, 20)
        processor = Processor(N=1, spline_kind="step_func")
        processor.add_ctrl(sigmaz())
        processor.tlist = tlist
        processor.coeffs = np.array([[np.sin(t) for t in tlist]])
        processor.plot_pulses(noisy=False)
        plt.clf()

        # cubic spline
        tlist = np.linspace(0., 2 * np.pi, 20)
        processor = Processor(N=1, spline_kind="cubic")
        processor.add_ctrl(sigmaz())
        processor.tlist = tlist
        processor.coeffs = np.array([[np.sin(t) for t in tlist]])
        processor.plot_pulses(noisy=False)
        plt.clf()

        # noisy
        processor = Processor(N=1)
        processor.add_ctrl(sigmaz(), targets=0)
        processor.add_ctrl(sigmay(), targets=0)
        processor.coeffs = np.array([[0.5, 0., 0.5], [0., 0.5, 0.]])
        processor.tlist = np.array(
            [0., np.pi / 2., 2 * np.pi / 2, 3 * np.pi / 2])

        processor.plot_pulses(noisy=False)
        plt.clf()
        processor.plot_pulses(noisy=True)
        plt.clf()
예제 #7
0
    def TestNoise(self):
        """
        Test for Processor with noise
        """
        # setup and fidelity without noise
        rho0 = qubit_states(2, [0, 0, 0, 0])
        tlist = np.array([0., np.pi / 2.])
        a = destroy(2)
        proc = Processor(N=2)
        proc.tlist = tlist
        proc.coeffs = np.array([1]).reshape((1, 1))
        proc.add_ctrl(sigmax(), targets=1)
        result = proc.run_state(rho0=rho0)
        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(rho0=rho0)
        assert_allclose(fidelity(result.states[-1],
                                 qubit_states(2, [0, 1, 0, 0])),
                        0.981852,
                        rtol=1.e-3)

        # white noise with internal/external operators
        proc.noise = []
        white_noise = RandomNoise(loc=0.1, scale=0.1)
        proc.add_noise(white_noise)
        result = proc.run_state(rho0=rho0)

        proc.noise = []
        white_noise = RandomNoise(loc=0.1, scale=0.1, ops=sigmax(), targets=1)
        proc.add_noise(white_noise)
        result = proc.run_state(rho0=rho0)