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

add ControlledGate class #2862

Merged
merged 28 commits into from
Nov 12, 2019
Merged

add ControlledGate class #2862

merged 28 commits into from
Nov 12, 2019

Conversation

ewinston
Copy link
Contributor

@ewinston ewinston commented Jul 25, 2019

Fixes #544, Fixes #2804

Summary

Adds a ControlledGate class inheriting from the Gate class. Also, adds a method q_if to the Gate class which returns a ControlledGate instance.

addresses part of issue #2804

Details and comments

@ewinston ewinston force-pushed the gatecontrol branch 3 times, most recently from b30ea21 to cfaff92 Compare August 7, 2019 19:47
@ewinston ewinston changed the title [WIP] add ControlledGate class add ControlledGate class Aug 8, 2019
@ajavadia ajavadia self-assigned this Aug 20, 2019
@dime10
Copy link
Contributor

dime10 commented Sep 27, 2019

I am working on an optimization pass which makes use of this new controlled gate class and I noticed the following feature is missing which would be quite useful for the optimizer (and maybe in general too):

  1. On a ControlledGate object, access to the base gate that is being controlled, no matter how it was created (e.g. via ToffoliGate(), XGate().q_if(2), XGate().q_if().q_if(), CnotGate().q_if(), etc). This is important to identify whether gates can be cancelled even if their control qubits differ. This could be a simple attribute that references the class of the uncontrolled gate (e.g. self.base_gate = XGate)

Two other things I noticed:

  1. When creating a CCCX gate using q_if, why is the _cccx definition given in multi_control_toffoli_gate not used? Additionally, the generated definitions for CCCX vary depending on the path to generate it: XGate().q_if(3) != XGate().q_if(2).q_if() = XGate().q_if().q_if().q_if() = ToffoliGate().q_if() != CnotGate().q_if(2).

  2. When printing a circuit, controlled gates created via q_if() aren't drawn as controlled gates, rather as a single box.

updated standard gates to return controlled version if it is also in the standard set.
minor commit

added "controlled_ops" parameter

gate rules of gate before applying controls.

pass controlled gate tests

changing to recursive control

broken point
passing mu1 and mu3

tests working...confusing num_ctrl_qubits

linting

fix indentation error

fix circular import for python 3.5 and 3.6.

more import fixes

put aqua q_if back in, linting

minor commit

minor commit

linting

another attempt to fix circular import across all pltforms.

disable cyclic-import pylint check

another attempt to remove cyclic import

This also eliminates defining q_if in standard extension gates.

forgot to add module add_control.py

fix style

method from qi
todo: convert the gates to ControlledGates

linting

style

simplify some tests

minor fix
ewinston and others added 2 commits October 2, 2019 16:19
todo: convert the gates to ControlledGates

linting

style

simplify some tests

minor fix

Co-authored-by: Shaohan Hu <shaohan.hu@ibm.com>
Co-authored-by: Manoel Marques <manoel@us.ibm.com>
Co-authored-by: Albert Frisch <alfr@de.ibm.com>
@ewinston
Copy link
Contributor Author

ewinston commented Oct 4, 2019

Hi @dime10,
I can add (1) as you suggested. For (2), this pr currently implements the "noancilla" mode from multi_control_toffoli_gate as it behaves in aqua where it came from. Point (3) is referenced in issue #2804, which is why it was mentioned this pr only addresses the non-visualization part. I'll create a new issue for the visualization part. Thanks for the suggestions and trying it out!

This adds "base_gate" as class attribute of ControlledGate which is a class reference.
@1ucian0 1ucian0 added on hold Can not fix yet and removed on hold Can not fix yet labels Oct 22, 2019
@mergify mergify bot merged commit 370b27e into Qiskit:master Nov 12, 2019
@mtreinish mtreinish added the Changelog: New Feature Include in the "Added" section of the changelog label Nov 12, 2019
ewinston added a commit to ewinston/qiskit that referenced this pull request Nov 13, 2019
Also remove some unused imports in test_controlled_gate.py.
mergify bot pushed a commit that referenced this pull request Nov 30, 2019
* added release notes for controlled gate class (#2862).

Also remove some unused imports in test_controlled_gate.py.

* remove pylint disable

* remove one more unused import

* change q_if to control 

in expectation of pr #3455

* update release notes

include imports in release notes.
@ewinston ewinston deleted the gatecontrol branch June 9, 2020 19:52
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
* added ControlledGate class

updated standard gates to return controlled version if it is also in the standard set.

* added assertions to some tests.

minor commit

added "controlled_ops" parameter

gate rules of gate before applying controls.

pass controlled gate tests

changing to recursive control

broken point

* added dmitri based decomposition from aqua

passing mu1 and mu3

tests working...confusing num_ctrl_qubits

linting

fix indentation error

fix circular import for python 3.5 and 3.6.

more import fixes

put aqua q_if back in, linting

minor commit

minor commit

linting

another attempt to fix circular import across all pltforms.

disable cyclic-import pylint check

another attempt to remove cyclic import

This also eliminates defining q_if in standard extension gates.

forgot to add module add_control.py

fix style

method from qi

* copied multi controlled gates from aqua

todo: convert the gates to ControlledGates

linting

style

simplify some tests

minor fix

* simplify code. catch rotation gates to avoid decomposition.

* copied multi controlled gates from aqua

todo: convert the gates to ControlledGates

linting

style

simplify some tests

minor fix

Co-authored-by: Shaohan Hu <shaohan.hu@ibm.com>
Co-authored-by: Manoel Marques <manoel@us.ibm.com>
Co-authored-by: Albert Frisch <alfr@de.ibm.com>

* simplify code. catch rotation gates to avoid decomposition.

* add reference to base controlled gate

This adds "base_gate" as class attribute of ControlledGate which is a class reference.

* add some tests. fix imports

* remove pdb

* linting

* add `to_gate` method to Instruction.

* lint

* linting

* linting

* add test for instruction_to_gate

* add phase argument when computing control matrix

* linting
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
…3465)

* added release notes for controlled gate class (Qiskit#2862).

Also remove some unused imports in test_controlled_gate.py.

* remove pylint disable

* remove one more unused import

* change q_if to control 

in expectation of pr Qiskit#3455

* update release notes

include imports in release notes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gate.control() Implement the q_if method in the Gate hierarchy
5 participants