add_control: check for predefined gates by type instead of name#3853
add_control: check for predefined gates by type instead of name#3853willhbang wants to merge 11 commits into
Conversation
|
@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.
@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.
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?
| if isinstance(operation, XGate) or ( | ||
| isinstance(operation, controlledgate.ControlledGate) and | ||
| operation.base_gate.name == 'x'): | ||
| isinstance(operation.base_gate, XGate)): |
There was a problem hiding this comment.
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.
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.
@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.
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
Unrolleralso seems to depend onname, but testing for type there seemed to break a lot of tests. Unroller's usage ofnameisn't affected by user input (within the context of add_control), so maybe it's moot for this PR.