question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a transform `get_unitary_matrix` to obtain matrix representations of circuits

See original GitHub issue

As suggested in #1458, it would be useful to have a transform to obtain the matrix representation of a quantum function. The transform should look and behave like the following pseudocode (similar to the draw or specs transform):

def get_unitary_matrix(qnode, wire_order=None):

    @wraps(qnode)
    def wrapper(*args, **kwargs):
        qnode.construct(args, kwargs)
     
        mat = qml.math.eye(2 ** num_wires)

        for op in qnode.qtape.operations:
            next_matrix = # construct matrix from op
            mat = qml.math.dot(next_matrix, mat)
    
        return mat

    return wrapper

This transform should be able to process all operations, including controlled gates on non-adjacent wires or wires in reverse order.

Note: this was written as a QNode transform because wire order is important (and we could extract that from the device). However, it could also be written as a tape or quantum function transform where the wire order must be passed explicitly every time.

The functions compute_matrix_from_ops_one_qubit and compute_matrix_from_ops_two_qubit contain some simple starting code. Once the new transform is implemented, usage of these should be replaced by the transform, as well as matrix computation in other parts of the code (such as SingleExcitation and DoubleExcitation tests).


List of tasks for completion:

  1. Create a quantum function transform that takes the device wires as an argument;
  2. Create a QNode transform that calls the quantum function transform;
  3. Replace the usage of compute_matrix_from_ops_one_qubit and compute_matrix_from_ops_two_qubit;
  4. Update the tests for the decomposition of the following operations to a new transform:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:21 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
ingstracommented, Aug 26, 2021

I see! Thanks. Yes it works 😃

2reactions
josh146commented, Jul 19, 2021

However it could also be written as a tape or quantum function transform where the wire order must be passed explicitly every time.

Oh! Actually, should we have both? The QNode function can just call this qfunc transform and pass the device wires as an argument.

This feels more flexible, since then you can use it even if you don’t have a device

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I obtain transformation matrix of a quantum circuit in ...
I need to draw a quantum circuit in Clifford+T library and obtain automatically its transformation matrix. Is there any feature on Qiskit in ......
Read more >
How to obtain the unitary matrix of a circuit in Qiskit with Code
In this tutorial we will see how to obtain the unitary matrix of any quantum circuit in Qiskit. In quantum computing unitary matrices...
Read more >
Matrix representation of quantum circuits - notations and gotchas
A case study by building a tensor network to match qiskit conventions.
Read more >
Matrix Representations - Changing Bases - UNCW
We first use brute force methods for relating basis vectors in one representation in terms of another one. Then we will show the...
Read more >
Operator Class - Qiskit
The Operator class is used in Qiskit to represent matrix operators acting on a ... Create an operator XX = Operator(Pauli('XX')) # Add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found