-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
add_control: check for predefined gates by type instead of name #3853
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
control
also needs checks for u1, u3, and cx.
[edited] May be this isn't necessary until Unroller
does the same but would be within the goal of this PR if it doesn't break anything.
@ewinston To make sure I understand, we should check that the operation is not u1, u3, or cx, so we can simply apply the control before we go through the trouble of unrolling into those bases? |
qc = QuantumCircuit(1, name='x') | ||
gate = qc.to_gate() | ||
|
||
self.assertIsNone(gate.control().definition) # Not a CnotGate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ewinston looks like a change in #3739, specifically here:
https://github.com/Qiskit/qiskit-terra/blob/025f6f9e73572a4aa608ac6e90bb1d7bc557e51f/qiskit/circuit/controlledgate.py#L85
is causing the definition get in this test to throw with
TypeError: can only concatenate list (not "NoneType") to list
because the gate generated from this circuit has no definition.
Is that ok/expected, since the gate here is degenerate? I originally added no operations to the test circuit, since it wouldn't be relevant to the testing the name bug. If so i'll add some arbitrary gate to the circuit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this special case, where the controlled gate has an empty _definition
, the definition
method of ControlledGate should perhaps also return the same. Could you add an
else:
return self._definition
to ControlledGate.definition
after the elif
clause to see if that resolves it?
qiskit/circuit/add_control.py
Outdated
isinstance(operation, controlledgate.ControlledGate) and | ||
operation.base_gate.name == 'x'): | ||
isinstance(operation.base_gate, XGate)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to add a case for cx below, but realized it's already handled here for arbitrary controls. Is it worth doing the same for the other gate checks below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be good to do for the rotation gates as well. It would be interesting to check if it reduces gate count.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ewinston gave this a shot in the commit below, but it's breaking add_control for RZ and CRZ gates. Not sure where to start investigating... any ideas where to dig further?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this breaks but if you remove the or
condition in _operation_has_base-gate
the issue seems to resolve. Then it's just isinstance(operation, gate)
like in original logic.
@ewinston Besides RZ, it looks like checking for the base gate to avoid unrolling did reduce gate count, at least for the tests I ran: https://www.diffchecker.com/DmorhS8x
|
Summary
Details and comments
Unroller
also seems to depend onname
, but testing for type there seemed to break a lot of tests. Unroller's usage ofname
isn't affected by user input (within the context of add_control), so maybe it's moot for this PR.