Skip to content

add ability to specify open controls.#3739

Merged
mergify[bot] merged 21 commits into
Qiskit:masterfrom
ewinston:control_word
Feb 19, 2020
Merged

add ability to specify open controls.#3739
mergify[bot] merged 21 commits into
Qiskit:masterfrom
ewinston:control_word

Conversation

@ewinston
Copy link
Copy Markdown
Contributor

@ewinston ewinston commented Jan 22, 2020

This PR adds the ability to control on '0' qubit states as described in issue #3726. Visualizations are not updated in this PR.

Summary

Details and comments

Add ability to specify control conditioned on a qubit being in the ground state. The state of the control qubits is represented by a decimal integer. For example:

  from qiskit import QuantumCircuit
  from qiskit.extensions.standard import XGate

  qc = QuantumCircuit(4)
  cgate = XGate().control(3, ctrl_state=6)
  qc.append(cgate, [0, 1, 2, 3])

Creates a four qubit gate where the fourth qubit gets flipped if the first qubit is in the ground state and the second and third qubits are in the excited state. If ctrl_state is None, the default, control is conditioned on all control qubits being excited.

Alternatively the ctrl_state may be specified as a bit string:

 cgate = XGate().control(3, ctrl_state='110').

@ewinston ewinston changed the title add ability specify open controls. add ability to specify open controls. Jan 22, 2020
@ajavadia ajavadia self-assigned this Jan 23, 2020
@ajavadia
Copy link
Copy Markdown
Member

can you make ctrl_state also accept a bitstring too, as it may be more natrual to specify like that ('110' or 6)

@Cryoris
Copy link
Copy Markdown
Collaborator

Cryoris commented Jan 30, 2020

@ajavadia What's the status on this PR? This implements a functionality we would like to use in #3714, which is part of the Aqua 0.7 goal

Copy link
Copy Markdown
Member

@ajavadia ajavadia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no tests for a controlled Unitary gate (specified by a matrix). Can you add one please?

There are no modifications to the ControlledGate class as far as I can tell. It would be nice if the ctrl_state was also stored in instances of ControlledGate, so that e.g. it could be visualized with open controls. The conjugation with X gates should be done when decomposing that controlled gate.

Comment thread qiskit/extensions/unitary.py Outdated
Comment thread test/python/circuit/test_controlled_gate.py Outdated
Comment thread qiskit/extensions/unitary.py
Comment thread qiskit/circuit/add_control.py Outdated

"""
import qiskit.extensions.standard as standard
if isinstance(operation, standard.RZGate) or operation.name == 'rz':
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from before, but why is Rz a special case here, and all other gates are further down?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is due to the global phase issue. When num_ctrl_qubits=1 CrzGate is properly retrieved. However when num_ctrl_qubits > 1, the algorithm computes the control gate from the definition of the base gate. Since RzGate is defined by U1Gate, this leads to the wrong phase. By reducing the number of controls by 1 and using instead the definition from CrzGate, which is phase correct, the proper controlled version is generated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so maybe a comment here so someone reading the code would know why this is the way it is

Comment thread qiskit/circuit/add_control.py Outdated
@ewinston
Copy link
Copy Markdown
Contributor Author

@ajavadia Controlled unitaries are tested in the method test_controlled_random_unitary.
I'll add ctrl_state as an attribute.

Comment thread qiskit/extensions/unitary.py Outdated
Copy link
Copy Markdown
Member

@ajavadia ajavadia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good, thanks.
Were you going to test open control unitaries too?
Also I just saw the test for controlled unitaries and this is how random unitaries are made:
UnitaryGate(scipy.stats.unitary_group.rvs(num_target), but a simpler way is to just use qiskit.quantum_info.random.random_unitary()

@mergify mergify Bot merged commit 016f7a1 into Qiskit:master Feb 19, 2020
@ajavadia ajavadia mentioned this pull request Feb 25, 2020
2 tasks
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
* add ability specify open controls.

This PR adds the ability to control on '0' qubit states.

* add release notes

* expected blank space

* allow string control state specification.

* linting

* linting

* remove f-string

* add ctrl_state keyword to standard extensions

* fix comments. add ctrl_state attribute to ControlledGate.

* add ctrl_state and test of open controlled unitary matrix generation

* minor bug fixes

* linting

* linting

Co-authored-by: Ali Javadi-Abhari <ajavadia@users.noreply.github.com>
@ewinston ewinston deleted the control_word branch April 6, 2021 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants