Exemplo n.º 1
0
 def _get_omega(self):
     if self._omega is None:
         n = self.drift_dyn_gen.shape[0] // 2
         omg = sympl.calc_omega(n)
         if self.oper_dtype == Qobj:
             self._omega = Qobj(omg, dims=self.dyn_dims)
             self._omega_qobj = self._omega
         elif self.oper_dtype == sp.csr_matrix:
             self._omega = sp.csr_matrix(omg)
         else:
              self._omega = omg
     return self._omega
Exemplo n.º 2
0
 def _get_omega(self):
     if self._omega is None:
         n = self.drift_dyn_gen.shape[0] // 2
         omg = sympl.calc_omega(n)
         if self.oper_dtype == Qobj:
             self._omega = Qobj(omg, dims=self.dyn_dims)
             self._omega_qobj = self._omega
         elif self.oper_dtype == sp.csr_matrix:
             self._omega = sp.csr_matrix(omg)
         else:
             self._omega = omg
     return self._omega
Exemplo n.º 3
0
    def get_omega(self):
        if self.omega is None:
            n = self.drift_dyn_gen.shape[0] // 2
            self.omega = sympl.calc_omega(n)

        return self.omega
Exemplo n.º 4
0
    def test_symplectic(self):
        """
        Optimise pulse for coupled oscillators with Symplectic dynamics
        assert that fidelity error is below threshold
        """
        g1 = 1.0
        g2 = 0.2
        A0 = Qobj(np.array([[1, 0, g1, 0], 
                           [0, 1, 0, g2], 
                           [g1, 0, 1, 0], 
                           [0, g2, 0, 1]]))
        A_rot = Qobj(np.array([
                            [1, 0, 0, 0],
                            [0, 1, 0, 0],
                            [0, 0, 0, 0],
                            [0, 0, 0, 0]
                            ]))
        
        A_sqz = Qobj(0.4*np.array([
                            [1, 0, 0, 0],
                            [0, -1, 0, 0],
                            [0, 0, 0, 0],
                            [0, 0, 0, 0]
                            ]))
                            
        A_c = [A_rot, A_sqz]
        n_ctrls = len(A_c)
        initial = identity(4)
        A_targ = Qobj(np.array([
                        [0, 0, 1, 0], 
                        [0, 0, 0, 1], 
                        [1, 0, 0, 0], 
                        [0, 1, 0, 0]
                        ]))
        Omg = Qobj(sympl.calc_omega(2))
        S_targ = (-A_targ*Omg*np.pi/2.0).expm()
    
        n_ts = 20
        evo_time = 10
        
        result = cpo.optimize_pulse(A0, A_c, initial, S_targ, 
                        n_ts, evo_time, 
                        fid_err_targ=1e-3, 
                        max_iter=200,
                        dyn_type='SYMPL',
                        init_pulse_type='ZERO', 
                        gen_stats=True)
        assert_(result.goal_achieved, msg="Symplectic goal not achieved")
        assert_almost_equal(result.fid_err, 0.0, decimal=2, 
                            err_msg="Symplectic infidelity too high")
        
        # Repeat with Qobj integration
        resultq = cpo.optimize_pulse(A0, A_c, initial, S_targ, 
                        n_ts, evo_time, 
                        fid_err_targ=1e-3, 
                        max_iter=200,
                        dyn_type='SYMPL',
                        init_pulse_type='ZERO', 
                        dyn_params={'oper_dtype':Qobj},
                        gen_stats=True)
        assert_(result.goal_achieved, msg="Symplectic goal not achieved "
                                        "(Qobj integration)")
        
        # Check same result is achieved using the create objects method
        optim = cpo.create_pulse_optimizer(A0, list(A_c), 
                        initial, S_targ,
                        n_ts, evo_time, 
                        fid_err_targ=1e-3, 
                        dyn_type='SYMPL',
                        init_pulse_type='ZERO', 
                        gen_stats=True)
        dyn = optim.dynamics

        p_gen = optim.pulse_generator
        init_amps = np.zeros([n_ts, n_ctrls])
        for j in range(n_ctrls):
            init_amps[:, j] = p_gen.gen_pulse()
        dyn.initialize_controls(init_amps)

        # Check the exact gradient
        func = optim.fid_err_func_wrapper
        grad = optim.fid_err_grad_wrapper
        x0 = dyn.ctrl_amps.flatten()
        grad_diff = check_grad(func, grad, x0)
        assert_almost_equal(grad_diff, 0.0, decimal=5,
                            err_msg="Frechet gradient outside tolerance "
                                    "(SYMPL)")

        result2 = optim.run_optimization()
        assert_almost_equal(result.fid_err, result2.fid_err, decimal=6, 
                            err_msg="Direct and indirect methods produce "
                                    "different results for Symplectic")
Exemplo n.º 5
0
    def get_omega(self):
        if self.omega is None:
            n = self.drift_dyn_gen.shape[0] // 2
            self.omega = sympl.calc_omega(n)

        return self.omega
Exemplo n.º 6
0
    def test_07_symplectic(self):
        """
        control.pulseoptim: coupled oscillators (symplectic dynamics)
        assert that fidelity error is below threshold
        """
        g1 = 1.0
        g2 = 0.2
        A0 = Qobj(
            np.array([[1, 0, g1, 0], [0, 1, 0, g2], [g1, 0, 1, 0],
                      [0, g2, 0, 1]]))
        A_rot = Qobj(
            np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))

        A_sqz = Qobj(0.4 * np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 0, 0],
                                     [0, 0, 0, 0]]))

        A_c = [A_rot, A_sqz]
        n_ctrls = len(A_c)
        initial = identity(4)
        A_targ = Qobj(
            np.array([[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]))
        Omg = Qobj(sympl.calc_omega(2))
        S_targ = (-A_targ * Omg * np.pi / 2.0).expm()

        n_ts = 20
        evo_time = 10

        result = cpo.optimize_pulse(A0,
                                    A_c,
                                    initial,
                                    S_targ,
                                    n_ts,
                                    evo_time,
                                    fid_err_targ=1e-3,
                                    max_iter=200,
                                    dyn_type='SYMPL',
                                    init_pulse_type='ZERO',
                                    gen_stats=True)
        assert_(result.goal_achieved,
                msg="Symplectic goal not achieved. "
                "Terminated due to: {}, with infidelity: {}".format(
                    result.termination_reason, result.fid_err))
        assert_almost_equal(result.fid_err,
                            0.0,
                            decimal=2,
                            err_msg="Symplectic infidelity too high")

        # Repeat with Qobj integration
        resultq = cpo.optimize_pulse(A0,
                                     A_c,
                                     initial,
                                     S_targ,
                                     n_ts,
                                     evo_time,
                                     fid_err_targ=1e-3,
                                     max_iter=200,
                                     dyn_type='SYMPL',
                                     init_pulse_type='ZERO',
                                     dyn_params={'oper_dtype': Qobj},
                                     gen_stats=True)
        assert_(resultq.goal_achieved,
                msg="Symplectic goal not achieved "
                "(Qobj integration). "
                "Terminated due to: {}, with infidelity: {}".format(
                    resultq.termination_reason, result.fid_err))

        # Check same result is achieved using the create objects method
        optim = cpo.create_pulse_optimizer(A0,
                                           list(A_c),
                                           initial,
                                           S_targ,
                                           n_ts,
                                           evo_time,
                                           fid_err_targ=1e-3,
                                           dyn_type='SYMPL',
                                           init_pulse_type='ZERO',
                                           gen_stats=True)
        dyn = optim.dynamics

        p_gen = optim.pulse_generator
        init_amps = np.zeros([n_ts, n_ctrls])
        for j in range(n_ctrls):
            init_amps[:, j] = p_gen.gen_pulse()
        dyn.initialize_controls(init_amps)

        # Check the exact gradient
        func = optim.fid_err_func_wrapper
        grad = optim.fid_err_grad_wrapper
        x0 = dyn.ctrl_amps.flatten()
        grad_diff = check_grad(func, grad, x0)
        assert_almost_equal(grad_diff,
                            0.0,
                            decimal=5,
                            err_msg="Frechet gradient outside tolerance "
                            "(SYMPL)")

        result2 = optim.run_optimization()
        assert_almost_equal(result.fid_err,
                            result2.fid_err,
                            decimal=6,
                            err_msg="Direct and indirect methods produce "
                            "different results for Symplectic")
Exemplo n.º 7
0
    np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))

#Squeeze Control
A_sqz = Qobj(
    dyn.sqz *
    np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))
dyn.ctrl_dyn_gen = [A_rot, A_sqz]
n_ctrls = dyn.get_num_ctrls()

dyn.initial = identity(4)

# Target
A_targ = Qobj(
    np.array([[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]))

Omg = Qobj(sympl.calc_omega(2))
print("Omega:\n{}\n".format(Omg))

S_targ = (-A_targ * Omg * np.pi / 2.0).expm()
dyn.target = S_targ
#S_targ = (Omg*A_targ*np.pi/2.0).expm()
print("Target S:\n{}\n".format(S_targ))

# Initialise the dynamics
init_amps = np.zeros([n_ts, n_ctrls])
for j in range(n_ctrls):
    init_amps[:, j] = p_gen.gen_pulse()
dyn.initialize_controls(init_amps)

f_ext = "{}_n_ts{}_ptype{}.txt".format(example_name, n_ts, cfg.pulse_type)
                    [0, 0, 0, 0]
                    ]))
dyn.ctrl_dyn_gen = [A_rot, A_sqz]  
n_ctrls = dyn.get_num_ctrls()

dyn.initial = identity(4)

# Target
A_targ = Qobj(np.array([
                [0, 0, 1, 0], 
                [0, 0, 0, 1], 
                [1, 0, 0, 0], 
                [0, 1, 0, 0]
                ]))
          
Omg = Qobj(sympl.calc_omega(2))
print("Omega:\n{}\n".format(Omg))

S_targ = (-A_targ*Omg*np.pi/2.0).expm()
dyn.target = S_targ
#S_targ = (Omg*A_targ*np.pi/2.0).expm()
print("Target S:\n{}\n".format(S_targ))


# Initialise the dynamics
init_amps = np.zeros([n_ts, n_ctrls])
for j in range(n_ctrls):
    init_amps[:, j] = p_gen.gen_pulse()
dyn.initialize_controls(init_amps)