Add a transform `get_unitary_matrix` to obtain matrix representations of circuits
See original GitHub issueAs 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:
- Create a quantum function transform that takes the device wires as an argument;
- Create a QNode transform that calls the quantum function transform;
- Replace the usage of
compute_matrix_from_ops_one_qubit
andcompute_matrix_from_ops_two_qubit
; - Update the tests for the decomposition of the following operations to a new transform:
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:21 (21 by maintainers)
I see! Thanks. Yes it works 😃
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