Skip to content

Move controlled gate tests from Aqua to Terra#3714

Merged
1ucian0 merged 27 commits into
Qiskit:masterfrom
Cryoris:missing_tests_for_gates
Feb 25, 2020
Merged

Move controlled gate tests from Aqua to Terra#3714
1ucian0 merged 27 commits into
Qiskit:masterfrom
Cryoris:missing_tests_for_gates

Conversation

@Cryoris
Copy link
Copy Markdown
Collaborator

@Cryoris Cryoris commented Jan 14, 2020

Summary

Some multi-controlled gates have been moved from Aqua to Terra without their tests. This PR adds all existing tests for these gates that exist in Aqua. This moves #3639 forward.

Details and comments

Tests for the following files exist currently in Aqua but not in Terra:

  • qc.mct: multi-control Toffoli gate
  • qc.mcu1: multi-control U1 gate
  • qc.mcr(x,y,z): multi-control rotation gates

This PR integrates the tests from Aqua's test/aqua/test_<gatename>.py in Terra's test/python/circuit/test_controlled_gate.py. Note, that also the relative phase Toffoli gates were moved to Terra without tests, but also Aqua doesn't have any and therefore none are included in this PR. This should be done in a future PR.

Also the docstrings for all tests were updated to be complete sentences and made a bit more descriptive for some tests than before.

register names are more expressive now, also removed names of
register, since this is not needed.
list comprehension was removed where the whole register could
be passed instead.
Copy link
Copy Markdown
Member

@1ucian0 1ucian0 left a comment

Choose a reason for hiding this comment

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

The test are very hard to understand. I would prefer simpler and cleaner tests. Is that doable?

Comment thread test/python/circuit/test_controlled_gate.py Outdated
@Cryoris
Copy link
Copy Markdown
Collaborator Author

Cryoris commented Jan 15, 2020

The test are very hard to understand. I would prefer simpler and cleaner tests. Is that doable?

For a given set of control qubits, the tests build the matrix representation for the operation via the unitary simulator and compare it against the theoretical matrix. That's also what other tests (such as test_controlled_u1 do, but generalized to more control qubits.
We could simplify the test by choosing a fixed set of control qubits or comparing against a statevector, not a matrix. But maybe it would be sufficient to add a descriptive docstring and more comments?

@Cryoris Cryoris requested a review from 1ucian0 January 16, 2020 18:28
@ewinston
Copy link
Copy Markdown
Contributor

Does this test something different than what's tested by test_controlled_standard_gates? If so, perhaps they can reuse _compute_control_matrix.

@Cryoris
Copy link
Copy Markdown
Collaborator Author

Cryoris commented Jan 30, 2020

Does this test something different than what's tested by test_controlled_standard_gates? If so, perhaps they can reuse _compute_control_matrix.

We can, with the functionality from #3739 😄

Comment thread test/python/circuit/test_controlled_gate.py
Comment thread test/python/circuit/test_controlled_gate.py Outdated
Comment thread test/python/circuit/test_controlled_gate.py Outdated
Comment thread test/python/circuit/test_controlled_gate.py Outdated
output_ctrl != input_ctrl)
self.assertTrue(
((output_ctrl == input_ctrl) and (output_target != cond_output)) or
output_ctrl != input_ctrl)
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.

Having different assert under a condition tells me that these should be two different tests. What do you think? Would it be possible to split these situations ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This test was already in the master branch (my editor probably changed the format without me noticing), I think these tests are from @ewinston. Probably the entire condition in the assert must be fulfilled, or can they be split?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I put this into a single test because it is a validation of a truth table; the clause just separates truth and false conditions.

Copy link
Copy Markdown
Member

@1ucian0 1ucian0 left a comment

Choose a reason for hiding this comment

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

It looks good! I had some small comments here and there.

@Cryoris Cryoris requested a review from 1ucian0 February 25, 2020 11:53
@1ucian0 1ucian0 merged commit 0e21020 into Qiskit:master Feb 25, 2020
@Cryoris Cryoris deleted the missing_tests_for_gates branch February 25, 2020 15:45
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
* port qc.mct tests

* port qc.mcr(x,y,z) tests

* port qc.mcu1 test

* update register names, remove unnecessary list comprehension

register names are more expressive now, also removed names of
register, since this is not needed.
list comprehension was removed where the whole register could
be passed instead.

* docstrings have complete sentences

* fix lint, remove "parameterized" dependency

* use @combine instead of @unpack@data(itertools))

* fix import order

* self.assertTrue(np.allclose to assert_allclose

* atol as np.allclose

* unfold test_multi_control_toffoli_matrix_dirty_ancillas modes

* no need for combine in test_multi_control_toffoli_matrix_clean_ancillas

* unify style

* import gates via allGates

* unfold test_all_inverses

* unfolding test_multi_control_toffoli_matrix

* simplification

* use _compute_control_matrix where possible

* fix lint

* use subtest more, add note that tests from Aqua

Co-authored-by: Luciano Bello <luciano.bello@ibm.com>
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