Esempio n. 1
0
 def get_fidelity_with(self,
                       target_state: Union[str, Qobj] = "ghz") -> float:
     """
     :param target_state: One of "ghz", "ghz_antisymmetric", "ground", and "excited"
     :return:
     """
     assert (self.solve_result is not None
             ), "solve_result attribute cannot be None (call solve method)"
     final_state = self.solve_result.states[-1]
     if target_state == "ghz":
         return fidelity(final_state,
                         self.ghz_state.get_state_tensor(symmetric=True))**2
     elif target_state == "ghz_antisymmetric":
         return fidelity(
             final_state,
             self.ghz_state.get_state_tensor(symmetric=False))**2
     elif target_state == "ground":
         return fidelity(final_state, tensor(*get_ground_states(self.N)))**2
     elif target_state == "excited":
         return fidelity(final_state,
                         tensor(*get_excited_states(self.N)))**2
     elif isinstance(target_state, Qobj):
         return fidelity(final_state, target_state)**2
     else:
         raise ValueError(
             f"target_state has to be one of 'ghz', 'ground', or 'excited', not {target_state}."
         )
    def __init__(self,
                 N: int,
                 V: float,
                 geometry: BaseGeometry,
                 t_list: np.ndarray,
                 ghz_state: BaseGHZState,
                 Omega_range: Tuple[float, float],
                 Delta_range: Tuple[float, float],
                 verbose: bool = False):
        self.verbose = verbose

        self.ti_evolving_qubit_system_kwargs = {
            'N': N,
            'V': V,
            'geometry': geometry,
            'ghz_state': ghz_state,
            't_list': np.linspace(0, t_list[1], 10)
        }
        self.psi_0 = tensor(get_ground_states(N))
        self.t_list = t_list

        self.required_steps = len(t_list) - 1
        # Actions for all ts except the end.

        self.recorded_steps = {
            'Omega': [],
            'Delta': [],
        }
        self.step_number = 0
        self.latest_evolving_qubit_system: Optional[
            TimeIndependentEvolvingQubitSystem] = None
        self.total_solve_time = 0

        self.action_normalisation = Omega_range, Delta_range
        self.action_space = gym.spaces.Box(low=np.array([0, 0],
                                                        dtype=np.float32),
                                           high=np.array([1, 1],
                                                         dtype=np.float32))
        # Using action_normalisation as PPO policy generates actions of order 1

        self.observation_space = gym.spaces.Discrete(self.required_steps)

        self._maximum_fidelity_achieved = 0.505
Esempio n. 3
0
    def __init__(self,
                 N: int,
                 V: float,
                 geometry: BaseGeometry,
                 Omega: Callable[[float], float],
                 Delta: Callable[[float], float],
                 t_list: np.ndarray,
                 ghz_state: BaseGHZState,
                 psi_0: Qobj = None):
        super().__init__(N, V, geometry)

        self.Omega = Omega
        self.Delta = Delta
        self.t_list = t_list

        self.psi_0 = tensor(get_ground_states(N) if psi_0 is None else psi_0)
        self.ghz_state = ghz_state

        self.solve_result: Optional[Result] = None
Esempio n. 4
0
    def plot_basis_states_overlaps(self,
                                   ax,
                                   plot_title: bool = True,
                                   plot_others_as_sum: bool = False):
        states = get_states(self.N) if not plot_others_as_sum else [
            get_excited_states(self.N),
            get_ground_states(self.N)
        ]
        fidelities = []

        plot_individual_orthogonal_state_labels = len(states) <= 4
        plotted_others = False
        for i, state in enumerate(tqdm(states)):
            label = get_label_from_state(state)
            state_product_basis_index = get_product_basis_states_index(state)
            # state_fidelities = [fidelity(tensor(state), _instantaneous_state) ** 2
            #                     for _instantaneous_state in self.solve_result.states]
            state_fidelities = [
                np.abs(_instantaneous_state.data.toarray().flatten()
                       [state_product_basis_index])**2
                for _instantaneous_state in self.solve_result.states
            ]

            if 'e' not in label or 'g' not in label:
                fidelities.append(state_fidelities)

            plot_label = r"$P_{" + f"{label.upper()[0]}" + "}$" \
                if plot_individual_orthogonal_state_labels or ('e' not in label or 'g' not in label) else 'Others'
            if plot_label == 'Others':
                if plotted_others:
                    plot_label = None
                else:
                    plotted_others = True

            ax.plot(
                self.t_list,
                state_fidelities,
                label=plot_label,
                color='g'
                if 'e' not in label else 'r' if 'g' not in label else 'k',
                linewidth=1 if 'e' not in label or 'g' not in label else 0.5,
                alpha=0.5)

        fidelities_sum = np.array(fidelities).sum(axis=0)
        ax.plot(self.t_list,
                fidelities_sum,
                label="$P_{E} + P_{G}$",
                color='C0',
                linestyle=":",
                linewidth=1,
                alpha=0.7)

        if plot_others_as_sum:
            others_sum = 1 - fidelities_sum

            ax.plot(self.t_list,
                    others_sum,
                    label=r"$\sum{\textrm{Others}}$",
                    color='C1',
                    linestyle=":",
                    linewidth=1,
                    alpha=0.7)

        ax.set_ylabel("Fidelity")
        if plot_title:
            ax.set_title("Fidelity with basis states")
        ax.set_ylim((-0.1, 1.1))
        ax.yaxis.set_ticks([0, 0.5, 1])

        # ax.legend()
        handles, labels = ax.get_legend_handles_labels()
        # sort both labels and handles by labels
        labels, handles = zip(*sorted(zip(labels, handles)))
        ax.legend(handles, labels)
Esempio n. 5
0
 def _get_components(self) -> Tuple[Qobj, Qobj]:
     return tensor(get_ground_states(self.N)), tensor(get_excited_states(self.N))