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

Allow wire_options per wire MPL drawers #6165

Closed
dwierichs opened this issue Aug 28, 2024 · 5 comments
Closed

Allow wire_options per wire MPL drawers #6165

dwierichs opened this issue Aug 28, 2024 · 5 comments
Labels
enhancement ✨ New feature or request

Comments

@dwierichs
Copy link
Contributor

dwierichs commented Aug 28, 2024

Feature details

On the level of mid-scale algorithms, it might be nice to differentiate between different "types" of wires, for example by coloring them differently, or giving them distinct line styles. As an example, in this demo on preparing matrix product states, there are auxiliary bond qubits and physical qubits, and coloring them differently would be a neat thing to do.

Implementation

This should be as simply as allowing a nested dictionary for wire_options with wires as keys and inner wire_options dictionaries. If such a nested dictionary is passed, we only need to iterate over it, instead of reusing the same for all wires.
To make the idea clear, here are some example inputs that already are supported, and some that should work after adding this feature:

@qml.qnode(qml.device("default.qubit"))
def node(x):
    qml.RX(x, 0)
    qml.Hadamard(1)
    qml.CNOT([0, 1])
    return qml.expval(Z(0) @ Y(1))

# Default drawing:
qml.draw_mpl(node)(0.52)
# Currently already supported: Change wire options for all wires:
wire_options = {"color": "orange"}
qml.draw_mpl(node, wire_options=wire_options)(0.52) # All wires are orange
# New feature: Only color the first wire orange
wire_options = {0: {"color": "orange"}}
# New feature: Only make the second wire bold
wire_options = {1: {"linewidth": 5}}
# New feature: Make the first wire dashed and the second teal
wire_options = {0: {"linestyle": "--"}, 1: {"color": "teal"}}

For a circuit with many wires, it would be annoying to have to pass information for all wires. That's why the following should also be supported:
Specify the wire_options for all wires at the same time, but allow single wire entries to be in that dictionary, refining the options for a given wire:

@qml.qnode(qml.device("default.qubit"))
def node(x):
    for w in range(10):
         qml.Hadamard(w) 
    return qml.expval(Z(0) @ Y(1))

# New feature: Make all wires blue and bold, except for wires 2 and 6, which are dashed and another color
wire_options = {"color": "blue", "linewidth": 5, 2: {"linestyle": "--", "color": "red"}, 2: {"linestyle": "--", "color": "orange"}}
qml.draw_mpl(node, wire_options=wire_options)(0.52)

Suggestion for an updated description in the docstrings:

wire_options (dict): matplotlib formatting options for the wire lines. In addition to options
    passed to matplotlib, this ``dict`` may contain entries with wire labels as keys   
    and ``dict``\ s as values, which contain line formatting options for the specific wire.

How important would you say this feature is?

1: Not important. Would be nice to have.

Additional information

This is really just a small cosmetic idea.

@dwierichs dwierichs added the enhancement ✨ New feature or request label Aug 28, 2024
@zazabap
Copy link
Contributor

zazabap commented Oct 25, 2024

Dear @dwierichs , I am new to the community and hope to find some good first issue to contribute. If this issue is not yet taken by other people, could I possibly work on it and later submit a PR? I worked a lot on MPS and hope to improve a bit on this part. Maybe later adding similar features to MERA related implementations.

@dwierichs
Copy link
Contributor Author

Hi @zazabap
Thanks for your interest in contributing, this is great! 🎉
I think this is a neat small extension that definitely could be a good first issue to work on.
As there is a small UI decision involved, namely how to realize the individual options for different wires, let me discuss this internally first :) I'll get back to you soon!

@dwierichs
Copy link
Contributor Author

@zazabap I discussed internally, and I added details to the issue description of how the user interface (UI) should look for this feature. Would you like to work on it? :) Let me know in case you have any questions!

@zazabap
Copy link
Contributor

zazabap commented Oct 26, 2024

Thanks a lot for the internal discussion! I will start a pull request and work on it. The rest of the fix and new features will attribute to #6486

albi3ro added a commit that referenced this issue Nov 8, 2024
**Context:**
On the level of mid-scale algorithms, it might be nice to differentiate
between different "types" of wires, for example by coloring them
differently, or giving them distinct line styles. As an example, in
PennyLaneAI/qml#1185 on preparing matrix product
states, there are auxiliary bond qubits and physical qubits, and
coloring them differently would be a neat thing to do.

**Description of the Change:**
Update the output wire_options that could change the line style and
color for circuit output.
```python
        @qml.qnode(qml.device("default.qubit"))
        def node(x):
            for w in range(5):
                qml.Hadamard(w) 
            return qml.expval(qml.PauliZ(0) @ qml.PauliY(1))

        # Make all wires cyan and bold, 
        # except for wires 2 and 6, which are dashed and another color
        wire_options = {"color": "cyan", 
                        "linewidth": 5, 
                        2: {"linestyle": "--", "color": "red"}, 
                        6: {"linestyle": "--", "color": "orange"}
                    }
        _,ax  = qml.draw_mpl(node, wire_options=wire_options)(0.52)
```

**Benefits:**
When complicated sates and quantum circuits diagram are created, wires
could be marked with different selections.

**Possible Drawbacks:**
N/A

**Related GitHub Issues:**
#6165

---------

Co-authored-by: David Wierichs <[email protected]>
Co-authored-by: ringo-but-quantum <>
Co-authored-by: Astral Cai <[email protected]>
Co-authored-by: Christina Lee <[email protected]>
@albi3ro
Copy link
Contributor

albi3ro commented Nov 8, 2024

Closed by #6486

@albi3ro albi3ro closed this as completed Nov 8, 2024
mudit2812 pushed a commit that referenced this issue Nov 11, 2024
**Context:**
On the level of mid-scale algorithms, it might be nice to differentiate
between different "types" of wires, for example by coloring them
differently, or giving them distinct line styles. As an example, in
PennyLaneAI/qml#1185 on preparing matrix product
states, there are auxiliary bond qubits and physical qubits, and
coloring them differently would be a neat thing to do.

**Description of the Change:**
Update the output wire_options that could change the line style and
color for circuit output.
```python
        @qml.qnode(qml.device("default.qubit"))
        def node(x):
            for w in range(5):
                qml.Hadamard(w) 
            return qml.expval(qml.PauliZ(0) @ qml.PauliY(1))

        # Make all wires cyan and bold, 
        # except for wires 2 and 6, which are dashed and another color
        wire_options = {"color": "cyan", 
                        "linewidth": 5, 
                        2: {"linestyle": "--", "color": "red"}, 
                        6: {"linestyle": "--", "color": "orange"}
                    }
        _,ax  = qml.draw_mpl(node, wire_options=wire_options)(0.52)
```

**Benefits:**
When complicated sates and quantum circuits diagram are created, wires
could be marked with different selections.

**Possible Drawbacks:**
N/A

**Related GitHub Issues:**
#6165

---------

Co-authored-by: David Wierichs <[email protected]>
Co-authored-by: ringo-but-quantum <>
Co-authored-by: Astral Cai <[email protected]>
Co-authored-by: Christina Lee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants