Skip to content

Conversation

@exAClior
Copy link
Contributor

@exAClior exAClior commented Aug 2, 2020

Related Issue

Fixes #3183.

Source of Error

In the original code, we added \qw and \qwx too aggressively. Whenever there is a line in the original diagram, we will try to insert \qw or \qwx accordingly. This causes the program to try to add the angle bracket thing above and below the two cnots mentioned in the issue to be recognized as wires.

Summary of Fix

Add the pruning process for adding \qw and \qwx. Need none wire item to be on the same row or previous row needs to be connected to the current one.

Output After Fix

Rerunning code outputs the following.

\Qcircuit @R=1em @C=0.75em {
 \\
 &                      &    &                &                &    & \\
 &\lstick{\text{0}}& \qw& \qw&\control \qw    &         \qw    & \qw&\qw\\
 &\lstick{\text{1}}& \qw& \qw&         \qw\qwx&\control \qw    & \qw&\qw\\
 &\lstick{\text{2}}& \qw& \qw&\targ    \qw\qwx&         \qw\qwx& \qw&\qw\\
 &\lstick{\text{3}}& \qw& \qw&         \qw    &\targ    \qw\qwx& \qw&\qw\\
 &                      &    &                &                &    & \\
 \\
}

Latex Compiled Output

Following are the Latex compiled output for the above code and one extra to show we don't over connect using \qwx
Screenshot_20200802_095335
Screenshot_20200802_095331

Misc

Seems to pass the qcircuit_test.py. But I fear that we might need other tests in the future? Please let me know if I did something wrong.

@googlebot googlebot added the cla: yes Makes googlebot stop complaining. label Aug 2, 2020
Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

Thank you for working on this!
I will have to look at the algo itself, but first it needs a test - I recommend reproducing the issue with the smallest possible example with a test that would fail without your fix. Then add in your fix. That should also remove the need for ignoring the coverage tool!

if row_has_item:
diagram2.write(2 * w - 1, y, r'&\qw\\')
else:
diagram2.write(2 * w - 1, y, r'& \\') # coverage: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

We should only ignore coverage in very exceptional scenarios where it's impossible/very expensive to test. I don't think this is that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the help! Sorry about the mistake here, I did not understand what the coverage tool was for fully. I suppose I would add the appropriate test into qcircuit_test.py? On a side note, I had some problems with not passing the formatting test. Is there a formatting tool everyone else is using for this project? I ran mypy and pylint with the appropriate configuration file. Neither of them complained about my coding style. Yet the formatting test gave me a hard time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you should write a new test method in qcircuit_test.py. It will contain a test circuit and the expected output similar to this:

def test_other_diagram():
a, b, c = cirq.LineQubit.range(3)
circuit = cirq.Circuit(cirq.X(a), cirq.Y(b), cirq.Z(c))
expected_diagram = r"""
\Qcircuit @R=1em @C=0.75em {
\\
&\lstick{\text{0}}& \qw&\targ \qw&\qw\\
&\lstick{\text{1}}& \qw&\gate{\text{Y}} \qw&\qw\\
&\lstick{\text{2}}& \qw&\gate{\text{Z}} \qw&\qw\\
\\
}""".strip()
assert_has_qcircuit_diagram(circuit, expected_diagram)

Regarding formatting - you can just run check/format-incremental --apply and that will reformat your files properly.

Copy link
Contributor

@balopat balopat Aug 3, 2020

Choose a reason for hiding this comment

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

btw feel free to jump on https://gitter.im/cirqdev/community if you want to chat

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot for the help! 👍 I ended up using yapf directly. The ./format-incremental --apply did not really work for me. Perhaps it would be nice for me to mention that by the end of CONTRIBUTING.md? I felt really bad spamming these commits that fail the format testing. Perhaps it would help future newbie's like me who wants to contribute?

Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

Thanks for the test! I added my comments, I think an alternative design can make this significantly simpler: we should just skip moment group generation for qcircuit. Unless there is a way to draw those brackets with qcircuit - which would be even cooler.

expected_diagram = r"""
\Qcircuit @R=1em @C=0.75em {
\\
& & & & & & \\
Copy link
Contributor

Choose a reason for hiding this comment

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

we shouldn't need to have an empty line here

&\lstick{\text{1}}& \qw& \qw& \qw\qwx&\control \qw & \qw&\qw\\
&\lstick{\text{2}}& \qw& \qw&\targ \qw\qwx& \qw\qwx& \qw&\qw\\
&\lstick{\text{3}}& \qw& \qw& \qw &\targ \qw\qwx& \qw&\qw\\
& & & & & & \\
Copy link
Contributor

Choose a reason for hiding this comment

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

we shouldn't need to have an empty line here either

# if has none wire before, should draw wire
post1 = r'\qw' if key in qw and row_has_item else ''
post2 = ''
if key in qwx:
Copy link
Contributor

Choose a reason for hiding this comment

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

This solution is interesting, but it is very hard to read and reason about.
The key issue is that the moment groups character is not handled well by qcircuit. So, I would rather introduce a by default True parameter to to_text_diagram_drawer, something like draw_moment_groups - and then the qcircuit drawer could set that to false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! I will get back to this ASAP but I just lost power here a couole hours ago. The storm hit us hard. Just a heads up that I might not be able to work at all in the next day or two

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries, no rush - good luck with the storm, stay safe!

Copy link
Contributor Author

@exAClior exAClior Aug 8, 2020

Choose a reason for hiding this comment

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

Hello @balopat, as I looked at your suggestion, I realized how silly my method is. I tried to keep the angle bracket to indicate the moment as I thought it is a really good reminder for viewers that these gates can be run simultaneously. But I hit a wall and got no good results. So, I basically did what you said, and added a test in qcircuit_test.py to account for the circuit that caused our initial problem #3183.
I have been trying to battle with the doc build error from CI but I did not even change the file it was mentioning. Then I went to change the file itself by just calling yapf on the mentioned code, it is still giving me an error. If you got time, could you please take a look at it? Thanks in advance for your time and help.

@exAClior exAClior requested a review from balopat August 8, 2020 19:50
@balopat
Copy link
Contributor

balopat commented Aug 10, 2020

Hi @exAClior - I suggest fixing somehow your branch - you seemed to pull in some commits from master incorrectly - the 3 files that you changed look good, but Github shows 26 files changed somehow.

The doc failures are unrelated, other PRs are breaking on it too - I'll fix them soon.

@balopat balopat added automerge Tells CirqBot to sync and merge this PR. (If it's running.) and removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) labels Aug 10, 2020
@balopat balopat added automerge Tells CirqBot to sync and merge this PR. (If it's running.) and removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) labels Aug 11, 2020
@exAClior exAClior closed this Aug 11, 2020
@exAClior exAClior reopened this Aug 11, 2020
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Aug 11, 2020
@CirqBot CirqBot merged commit 059006d into quantumlib:master Aug 11, 2020
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Aug 11, 2020
@exAClior exAClior deleted the qc-render-fix branch August 11, 2020 18:57
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this pull request Aug 23, 2020
### Related Issue
Fixes quantumlib#3183. 
### Source of Error
In the original code, we added \qw and \qwx too aggressively. Whenever there is a line in the original diagram, we will try to insert \qw or \qwx accordingly. This causes the program to try to add the angle bracket thing above and below the two cnots mentioned in the issue to be recognized as wires.
### Summary of Fix
Add the pruning process for adding \qw and \qwx. Need none wire item to be on the same row or previous row needs to be connected to the current one.
### Output After Fix
Rerunning code outputs the following. 

    \Qcircuit @r=1em @C=0.75em {
     \\
     &                      &    &                &                &    & \\
     &\lstick{\text{0}}& \qw& \qw&\control \qw    &         \qw    & \qw&\qw\\
     &\lstick{\text{1}}& \qw& \qw&         \qw\qwx&\control \qw    & \qw&\qw\\
     &\lstick{\text{2}}& \qw& \qw&\targ    \qw\qwx&         \qw\qwx& \qw&\qw\\
     &\lstick{\text{3}}& \qw& \qw&         \qw    &\targ    \qw\qwx& \qw&\qw\\
     &                      &    &                &                &    & \\
     \\
    }
### Latex Compiled Output
Following are the Latex compiled output for the above code and one extra to show we don't over connect using \qwx
![Screenshot_20200802_095335](https://user-images.githubusercontent.com/10986218/89124597-4675b400-d4a6-11ea-9fcb-44482ecdbb95.png)
![Screenshot_20200802_095331](https://user-images.githubusercontent.com/10986218/89124599-4675b400-d4a6-11ea-9bba-bf78de2bb9f1.png)

### Misc
Seems to pass the qcircuit_test.py. But I fear that we might need other tests in the future? Please let me know if I did something wrong.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Makes googlebot stop complaining.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QCircuit does not render gates that cross each other correctly

4 participants