Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the way gates are applied by the GateApplicator objects. #203

Open
shahidee44 opened this issue Mar 13, 2023 · 0 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@shahidee44
Copy link
Collaborator

shahidee44 commented Mar 13, 2023

I don't think we need 4 different methods (apply_1q_rotation_gate, apply_2q_rotation_gate, apply_1q_fixed_gate, apply_1q_fixed_gate) I think they all do exactly the same.

I believe we could change all of this:

    @staticmethod
    def apply_1q_rotation_gate(
        vectorized_gate: Callable,
        qubit_1: int,
        rotation_object: RotationAngle
    ):
        vectorized_gate(
            qubit_1,
            rotation_object.rotation_angle
        )

    @staticmethod
    def apply_2q_rotation_gate(
        vectorized_gate: Callable,
        qubit_1: int,
        qubit_2: int,
        rotation_object: RotationAngle
    ):
        vectorized_gate(
            qubit_1,
            qubit_2,
            rotation_object.rotation_angle
        )

    @staticmethod
    def apply_1q_fixed_gate(
        vectorized_gate: Callable,
        qubit_1: int
    ):
        vectorized_gate(
            qubit_1
        )

    @staticmethod
    def apply_2q_fixed_gate(
        vectorized_gate: Callable,
        qubit_1: int,
        qubit_2: int
    ):
        vectorized_gate(
            qubit_1,
            qubit_2
        )

    def apply_gate(self, gate: gates_core.Gate, *args):
        selected_vector_gate = self.gate_selector(gate, args[-1])
        # Remove argument from tuple
        args=args[:-1]
        if gate.n_qubits == 1:
            if hasattr(gate, 'rotation_object'):
                # *args must be of the following format -- (qubit_1,rotation_object)
                self.apply_1q_rotation_gate(selected_vector_gate, *args)
            else:
                # *args must be of the following format -- (qubit_1)
                self.apply_1q_fixed_gate(selected_vector_gate, *args)
        elif gate.n_qubits == 2:
            if hasattr(gate, 'rotation_object'):
                # *args must be of the following format -- (qubit_1,qubit_2,rotation_object)
                self.apply_2q_rotation_gate(selected_vector_gate, *args)
            else:
                # *args must be of the following format -- (qubit_1,qubit_2)
                self.apply_2q_fixed_gate(selected_vector_gate, *args)
        else:
            raise ValueError("Error applying the requested gate. Please check in the input")

for something much more compact like:

    def apply_gate(self, gate: gates_core.Gate, *args):

        if not gate.n_qubits in [1, 2]:
            raise ValueError("Error applying the requested gate. Please check in the input")
        
        selected_vector_gate = self.gate_selector(gate, args[-1])

        args_ = args[:-1]
        for i in range(len(args_)):
            if isinstance(args_[i], RotationAngle):
                args_[i] = args_[i].rotation_angle
                
        selected_vector_gate(*args_)

Originally posted by @raulconchello in #193 (comment)

@Q-lds Q-lds added the enhancement New feature or request label Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants