Skip to content
New issue

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

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

Already on GitHub? # to your account

Update the type hints for QuantumCircuit.append #13617

Open
alexanderivrii opened this issue Jan 7, 2025 · 4 comments
Open

Update the type hints for QuantumCircuit.append #13617

alexanderivrii opened this issue Jan 7, 2025 · 4 comments
Assignees
Labels
documentation Something is not clear or an error documentation good first issue Good for newcomers
Milestone

Comments

@alexanderivrii
Copy link
Contributor

The QuantumCircuit.append method looks as follows

    def append(
        self,
        instruction: Operation | CircuitInstruction,
        qargs: Sequence[QubitSpecifier] | None = None,
        cargs: Sequence[ClbitSpecifier] | None = None,
        *,
        copy: bool = True,
    ) -> InstructionSet:

with the instruction being appended to a quantum circuit being of type Operation | CircuitInstruction.

In practice, however, we can add also anything that has a to_instruction method (see also this comment). It would be nice to reflect this functionality in the type hints and/or in the following docstring.

@alexanderivrii alexanderivrii added documentation Something is not clear or an error documentation good first issue Good for newcomers labels Jan 7, 2025
@alexanderivrii alexanderivrii added this to the 2.0.0 milestone Jan 7, 2025
@github-project-automation github-project-automation bot moved this to Tagged but unassigned in Contributor Monitoring Jan 7, 2025
@Ak-ash22
Copy link
Contributor

Hello... looks like this is issue is still free for grabs. May I take this up?

@alexanderivrii
Copy link
Contributor Author

Thank you! You are now assigned to the task.

@AngeloDanducci AngeloDanducci moved this from Tagged but unassigned to Assigned in Contributor Monitoring Jan 16, 2025
@Ak-ash22
Copy link
Contributor

Ak-ash22 commented Jan 20, 2025

Hello @alexanderivrii , I am thinking of modifying things the following way:

  1. type hints from instruction: Operation | CircuitInstruction to instruction: InstructionLike
  2. and docstring to include additionally in the , :class: ~.circuit would be converted into .circuit.Instruction. and include in the Args::class with .to_instruction() attribute.

What do you suggest about this? If you have a better way of putting things, kindly let me know.

@Gyan-max
Copy link

Gyan-max commented Feb 8, 2025

Hi @Ak-ash22 and @alexanderivrii,

I appreciate the approach of introducing InstructionLike for better type hinting in QuantumCircuit.append. It makes sense to support objects with a to_instruction() method explicitly.

A few thoughts:

Would it be beneficial to include Union[Operation, CircuitInstruction, HasToInstruction] instead of a new alias (InstructionLike)? This would keep things explicit while maintaining flexibility.
Updating the docstring to clarify that any object with a .to_instruction() method is accepted is a great idea. Maybe we can also add a small example in the documentation?
Let me know if I can assist in testing or reviewing the changes!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
documentation Something is not clear or an error documentation good first issue Good for newcomers
Projects
Status: Assigned
Development

No branches or pull requests

3 participants