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 gate class #96

Merged
merged 17 commits into from
May 25, 2022
Merged

Add gate class #96

merged 17 commits into from
May 25, 2022

Conversation

BoxiLi
Copy link
Member

@BoxiLi BoxiLi commented Oct 2, 2021

The first step of refactoring the gate representation. It defines a gate class for each gate function as a subclass of Gate, this simplifies several things:

  • Using the gate class will allow one to identify the gate type through isinstance and issubclass, instead of by the name attribute.
  • Get rid of several look-up tables in several different places.
  • Allow a more natural way of defining custom gates. One only needs to define one's own gate class with a get_compact_operator method.
  • (To be implemented in a different PR) Make the circuit representation much cleaner, in particular gate decomposition. Each gate decomposition should be implemented inside the gate class, instead of in circuit.py

Docstrings are also added to each gate class with examples.

See also #15 for more details.

There is a few codeclimate issues regarding Line too long and Similar blocks. I don't plan to fix them because the similar gate class are by design and the long lines are just docstrings with code example output. They are better like this.

@BoxiLi BoxiLi force-pushed the circuit_refactor branch 4 times, most recently from 508c6bb to 7cf287e Compare March 29, 2022 20:37
@BoxiLi BoxiLi marked this pull request as ready for review March 29, 2022 20:41
@@ -179,55 +177,6 @@ def test_add_gate(self):
assert qc.gates[6].targets == [5]
assert qc.gates[5].arg_value == 1.570796

# Test Exceptions # Global phase is not included
Copy link
Member Author

Choose a reason for hiding this comment

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

These tests are removed because they are too detailed and only checking ValueError without the error message also does not correctly check if the exception raised is the correct one. In addition, in the new gate class structure, the input control is moved to each class.

BoxiLi added 15 commits May 22, 2022 17:32
The classical control value does not yet work if the control_value is a tuple.
They are the same gates and hence should not have different names.
This is another example that shows that using name to identify gate is not a good approach.
If isinstance  is used for the class name, the result will always be consistent.
It is more common to put control before target, such as CNOT(0, 1)
These tests are removed because they are too detailed and only checking ValueError without the error message also does not correctly check if the exception raised is the correct one. In addition, in the new gate class structure, the input control is moved to each class.
Disable "Line too long", which is already covered in Black.
This makes sure sure that gate defined using CSIGN will always be recognized by isinstance(gate, CSIGN).
To compare the generated gate it is better to use the fidelity measure.
@BoxiLi BoxiLi merged commit 546a212 into qutip:master May 25, 2022
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.

None yet

1 participant