-
Notifications
You must be signed in to change notification settings - Fork 621
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
Single qbtgates changes #1224
Single qbtgates changes #1224
Conversation
7388e6e
to
c14f610
Compare
Notebook added |
These tests seem a little too scant in my opinion, I am not fully satisfied with the final result. They do not provide full coverage of these new functionalities. I am thinking of refactoring the whole thing and start all over again. |
@MartinSandeCosta well, it is most important that you are happy with PR. Let us know when you have something you want us to review. Thanks |
A little more detail in the opening comment is preferred. It helps whoever has to update the changelog. |
@MartinSandeCosta, free feel to complete the tests in the way you want. However, maybe it is good to discuss a bit with @jakelishman? Since there is a refactoring of Besides, it is better to leave a more detailed description in the PR. As the template indicated:
|
Hmmm. You're right that there will be some nasty merge conflicts with #1181, but I don't see anyway around those, to be honest without that PR being merged before this one (which doesn't look very likely). Barring #1181 getting merged in the next day or two, I'd just go ahead with these commits as written. If the new-style tests were merged, qutip/qutip/tests/test_gates.py Lines 222 to 239 in 77c21f8
pytest.param(gates.x_gate, 0, "X"),
pytest.param(gates.y_gate, 0, "Y"),
... (and we'd probably change the function name to qutip/qutip/tests/test_gates.py Lines 241 to 259 in 77c21f8
Overall, it's clear that you can't write sensible new-style tests without #1181 having been merged, so I wouldn't worry about it too much. I did drop in a couple of comments in-line (minor nitpicking), but given that this file may well get pretty much overwritten - keeping the actual tests, of course! - in short order, I wouldn't consider them changes that need to be made, unless Boxi asks. Also, as you noted, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are just nitpicks, and a couple of comments on how we might have done things in the new pytest
way. In general I think you did a very good job of writing code that's presumably not your natural style, but is well in keeping with everything else in the files!
G = g(N, m) | ||
psi_res = G * psi_in | ||
|
||
assert_((psi_out - psi_res).norm() < 1e-12) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I know you're just following what's going on in the rest of the file (and that's typically a very good thing!). Now that we've moved to using pytest
, np.testing.assert_
is unnecessary - you can just use the keyword assert
. It used to be used because numpy abstracted their own tests away from the framework that they used to run them (sort of), and then this provided consistency. Nowadays, pytest
will do introspection on any variables that appear in a bare assert
statement and print out their values, so we actually get much better error reporting by just doing assert
.
|
||
assert_(qc1.input_states[2] == "0") | ||
assert_(qc1.input_states[3] == "0") | ||
assert_(qc1.input_states[6] == "0") | ||
assert_(qc1.input_states[1] == "+") | ||
assert_(qc1.input_states[4] == "+") | ||
|
||
assert_(qc1.output_states[2] == None) | ||
assert_(qc1.output_states[2] is None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we were to keep using the np.testing
functions (which we don't need to because pytest
makes it all easier), the better one here would be np.testing.assertEqual
(even for the None
test).
We always should think about what information we want to get out of a failed test. In this case, we'd be best served by knowing what the incorrect value was, so we should pass the two parts of the comparison separately. That way, np.testing.assertEqual
will print them out on a test failure, whereas this way np.testing.assert_
just gets fed the value False
and we wouldn't know what it was (was it 3, 0, "", "None"?) and we'd have to do more work offline to find out.
def test_exceptions(self): | ||
""" | ||
Text exceptions are thrown correctly for inadequate inputs | ||
""" | ||
qc = QubitCircuit(2) | ||
for gate in ["X", "Y", "Z", "S", "T"]: | ||
assert_raises(ValueError, qc.add_gate, gate, | ||
targets=[1], controls=[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In new pytest
-style, the for
loop would be done with parametrised tests and the raising test uses the pytest.raises
context manager, so you get easier-to-read code (and it's selectable if you only want to test a certain gate) like
@pytest.mark.parametrize('gate', ['X', 'Y', 'Z', 'S' ,'T'])
def test_exceptions(self, gate):
qc = QubitCircuit(2)
with pytest.raises(ValueError):
qc.add_gate(gate, targets=[1], controls=[0])
Again, thinking of the test log, this way is nice because the test log will tell you exactly which gate failed - pytest
adds the parametrised arguments into the test name.
Thank you for taking the time to look at these changes, @jakelishman! It must have been a true nightmare looking at my code haha 😄 Now on a more formal note. Regarding the pytest side of things, should I reformat the tests (related to my single_qubit implementation) in test_qubitcircuit.py and test_gates to follow the changes in #1181? It would definitely make it easier to merge them later on. |
Nah, don't disparage yourself - your code is good and you've done well to copy the style of the rest of the files. Typically consistency is better than having "the one true style"; code's read more than it's written, and having a module which is clearly written by four different people makes the logic very very difficult to follow, which in turns makes it much harder to spot bugs. If it were up to me (which is isn't, really), I would say that your changes are good as they are. I didn't mean for the comments to be changes you had to make. You can't reduce the number of merge conflicts that are going to happen with #1181 anyway, because that PR is a massive refactor of large swathes of the testing suite. You've done a good job to keep your work consistent with the rest of the file, so it will be very easy to merge in your logical changes. Merge conflicts are pretty much just par for the course; just the fact that we're discussing this here shows that logically, there is a conflict between two pieces of work being done at the same time. I'd suggest merging this as-is. We'll handle the problems caused by the refactor elsewhere. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @MartinSandeCosta, you've done very well. As @jakelishman said, keeping thing consistent with the rest is definitely good. It's just because there have been several waves of development of QuTiP and the coding style is not always consistent with each other, as you may already see. And it happens that @jakelishman is modernizing the tests right at this moment.
There is an API change that we'd better not do in this PR, otherwise, everything is good. I'll merge it then and we can deal with the conflicts in the tests later. Good job!
8a844d1
to
9f7f2e6
Compare
@MartinSandeCosta Congrats! By the way, the changelog in the PR description will later be used in the documentation (http://qutip.org/docs/latest/changelog.html). It's better to be a summary of the PR in one-line :) |
Description
Related issues or PRs
fixes #1225
Changelog
The controlled version of the single-qubit gates and tests