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

Drawing a ControlledQubitUnitary #1174

Merged
merged 3 commits into from
Mar 29, 2021
Merged

Drawing a ControlledQubitUnitary #1174

merged 3 commits into from
Mar 29, 2021

Conversation

antalszava
Copy link
Contributor

@antalszava antalszava commented Mar 26, 2021

Context:
We consider drawing a ControlledQubitUnitary using the circuit drawer, which raises an error due to having no special rule for drawing this operation.

Description of the Change:
Adds a rule for rendering the matrix and the operation.

Benefits:
Draws ControlledQubitUnitary instances.

Possible Drawbacks:
N/A

Related GitHub Issues:
Closes #1171

@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit .github/CHANGELOG.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@codecov
Copy link

codecov bot commented Mar 26, 2021

Codecov Report

Merging #1174 (d92c6bc) into master (d0d4f81) will increase coverage by 3.31%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1174      +/-   ##
==========================================
+ Coverage   94.81%   98.12%   +3.31%     
==========================================
  Files         144      144              
  Lines       10836    10847      +11     
==========================================
+ Hits        10274    10644     +370     
+ Misses        562      203     -359     
Impacted Files Coverage Δ
...ennylane/circuit_drawer/representation_resolver.py 99.33% <100.00%> (+0.04%) ⬆️
pennylane/ops/qubit.py 98.30% <100.00%> (+<0.01%) ⬆️
pennylane/math/tensorbox.py 96.02% <0.00%> (+1.32%) ⬆️
pennylane/beta/devices/default_tensor.py 95.22% <0.00%> (+1.70%) ⬆️
pennylane/devices/default_qubit_tf.py 90.66% <0.00%> (+2.66%) ⬆️
pennylane/math/numpy_box.py 98.57% <0.00%> (+2.85%) ⬆️
pennylane/beta/devices/default_tensor_tf.py 87.50% <0.00%> (+3.12%) ⬆️
pennylane/qnode.py 98.67% <0.00%> (+3.96%) ⬆️
pennylane/interfaces/torch.py 100.00% <0.00%> (+4.81%) ⬆️
pennylane/interfaces/tf.py 97.14% <0.00%> (+5.71%) ⬆️
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d0d4f81...d92c6bc. Read the comment docs.

Comment on lines +371 to +377
elif base_name == "ControlledQubitUnitary":
if wire in op.control_wires:
return self.charset.CONTROL

representation = RepresentationResolver._format_controlled_qubit_unitary(
op, "U", self.unitary_matrix_cache
)
Copy link
Member

Choose a reason for hiding this comment

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

Nice :)

Comment on lines +1911 to +1913
# Saving for the circuit drawer
self.control_wires = control_wires
self.U = U
Copy link
Member

Choose a reason for hiding this comment

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

If these attributes are required by the circuit drawer, we should probably document it somewhere.

Are there any other operations that are missing these attributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are there any other operations that are missing these attributes?

They seem to be specific to ControlledQubitUnitary (at least with the current structure of PennyLane). Storing these two are required because:

  • control_wires is merged into self.wires with the target wires at a later stage;
  • the unitary is expanded to the entire subsystem using identities on the control qubits and only the expanded unitary is stored in self.data.

Therefore, introducing self.control_wires and self.U provides a convenient way to gather both the control wires and the unitary.

We could actually consider separating control and target wires natively for other more defined operations, as it can be convenient to have the separation be available easily.

If these attributes are required by the circuit drawer, we should probably document it somewhere.

Indeed, at the moment they are well used by the circuit drawer, but it can easily be that other features will also use them. Any thoughts on where they could be documented? For attributes of operations, we don't seem to have any specific docstrings/docs.

Copy link
Member

Choose a reason for hiding this comment

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

Any thoughts on where they could be documented?

This is a good question which I don't think has a good answer 🤔

Copy link
Contributor

@DSGuala DSGuala left a comment

Choose a reason for hiding this comment

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

Great!

@co9olguy co9olguy merged commit 3212c71 into master Mar 29, 2021
@co9olguy co9olguy deleted the draw_ctrl_qubit_unit branch March 29, 2021 16:59
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.

[BUG] Drawing a ControlledQubitUnitary returns TypeError
4 participants