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

being able to look up InstructionProperties from Instruction (not name) in Target #8892

Open
ajavadia opened this issue Oct 13, 2022 · 4 comments · May be fixed by #9158
Open

being able to look up InstructionProperties from Instruction (not name) in Target #8892

ajavadia opened this issue Oct 13, 2022 · 4 comments · May be fixed by #9158
Labels
type: feature request New feature or request

Comments

@ajavadia
Copy link
Member

What should we add?

Here's a small Target. The Operation names differ from the string by which they are keyed in the Target.

import math
from qiskit.transpiler import Target, InstructionProperties
from qiskit.circuit.library import RZZGate
from qiskit.circuit import Parameter

rzz_30_props = {
    (0, 1): InstructionProperties(duration=0.74e-7, error=.00002)
}

rzz_45_props = {
    (0, 1): InstructionProperties(duration=1.04e-6, error=.0002)
}

target = Target()
target.add_instruction(RZZGate(math.pi / 6), rzz_30_props, name='rzz_30')
target.add_instruction(RZZGate(math.pi / 4), rzz_45_props, name='rzz_45')

So if I want to know the gates I have available on (0,1), I could do, target.operations_for_qargs((0,1)), which gives:

[Instruction(name='rzz', num_qubits=2, num_clbits=0, params=[0.5235987755982988]),
 Instruction(name='rzz', num_qubits=2, num_clbits=0, params=[0.7853981633974483])]

But to look up the error for any of these, it seems like I need to know the associated key in the Target which is difficult.
If I try to get that via, target.operation_names_for_qargs((0,1)) I get a set which is not ordered like the above Instructions.
{'rzz_30', 'rzz_45'}

It would be helpful to be able to go directly from an Instruction to its error rate, and not have to go via the Target key, which may be some arbitrary string even.

@ajavadia ajavadia added the type: feature request New feature or request label Oct 13, 2022
@mtreinish
Copy link
Member

mtreinish commented Oct 13, 2022

So you can do:

names = target.operation_names_for_qargs((0, 1))
operation_and_errors = [(target.operation_from_name(name), target[name][(0, 1)].error) for name in names]

there is also the target.instructions property which returns a list of tuples of the operation objects and the qargs it applies to. That can be paired with the instruction_properties() method to get the properties of a particular index
https://qiskit.org/documentation/stubs/qiskit.transpiler.Target.instruction_properties.html#qiskit.transpiler.Target.instruction_properties
which I think we added because you requested it for a similar use case in #5885.

The one constraint in general is that the name is the canonical reference to operations in the target, and each operation defined in the target needs a unique name accordingly. So if we work with anything other than the name it will be an O(n) lookup cost to find the operation we need.

That being said we should support the use case of lookup needed here, can you describe what you want the lookup method to look like and what the return is I think we can easily add this for 0.23.0.

@ajavadia
Copy link
Member Author

there is also the target.instructions property which returns a list of tuples of the operation objects and the qargs it applies to. That can be paired with the instruction_properties() method to get the properties of a particular index
https://qiskit.org/documentation/stubs/qiskit.transpiler.Target.instruction_properties.html#qiskit.transpiler.Target.instruction_properties
which I think we added because you requested it for a similar use case in #5885.

I think I'm realizing now that instruction_properties() would be more useful if it took an Instruction. Because as it stands, you have to know the global index for that instruction, which in my case at least I'm first filtering some instructions then trying to look up their error.

I agree the lookup will be O(n) and we should document that. But I think having the option is useful. For now I'll use the first solution you proposed.

@mtreinish
Copy link
Member

mtreinish commented Oct 13, 2022

Just thinking out loud would the interface for instruction_supported: https://qiskit.org/documentation/stubs/qiskit.transpiler.Target.instruction_supported.html#qiskit.transpiler.Target.instruction_supported
but instead of returning a boolean to indicate the target supports that instruction we had it return the properties?

So it would be something like target.lookup_instruction_properties(operation_class=RZZGate, qargs=(0, 1), parameters=[pi / 6]) and have it return the InstructionProperties

@ajavadia
Copy link
Member Author

yeah that works too. and raise KeyError if not available.

mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Nov 17, 2022
This commit adds 6 new helper methods to the Target class. These methods
are used  to query the target by either name or class to extract the
InstructionProperties (or attributes of them) for a given instruction.
Previously doing this by name was possible using the mapping protocol,
but handling the combination of ideal gates with ``None`` at different
levels in the internal dictionaries was cumbersome and error prone.
These methods give a simple entry point to do the query which abstracts
away the internal structure of the target.

For the methods which use a class based lookup this wasn't easily
accomplishable before, the closest available method would just return a
boolean about whether the class was supported on the target or not.

Implements Qiskit#8892
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants