amazon-braket / amazon-braket-sdk-python

A Python SDK for interacting with quantum devices on Amazon Braket
https://aws.amazon.com/braket/
Apache License 2.0
294 stars 118 forks source link

Unitary doesnt accept int param for single qubit unitary matrix #997

Closed Manvi-Agrawal closed 4 weeks ago

Manvi-Agrawal commented 1 month ago

Describe the bug A clear and concise description of what the bug is. Cuurently circ = Circuit().unitary(matrix=np.array([[0, 1],[1, 0]]), targets=0) is compile-time error on dev variant based on branch in

https://github.com/amazon-braket/amazon-braket-sdk-python/pull/993

To reproduce A clear, step-by-step set of instructions to reproduce the bug.

Expected behavior A clear and concise description of what you expected to happen.

Screenshots or logs If applicable, add screenshots or logs to help explain your problem.

System information A description of your system. Please provide:

Additional context Add any other context about the problem here. See https://github.com/amazon-braket/amazon-braket-sdk-python/pull/993#discussion_r1628310002

peterkomar-aws commented 4 weeks ago

Hi @Manvi-Agrawal , thanks for pointing this out. You are right that the following code

import numpy as np
from braket.circuits import Circuit

circ = Circuit().unitary(matrix=np.array([[0, 1],[1, 0]]), targets=0)

raises the following TypeError:

...
-> 3621 if 2 ** len(targets) != matrix.shape[0]:
   3622     raise ValueError("Dimensions of the supplied unitary are incompatible with the targets")
   3624 return Instruction(Unitary(matrix, display_name), target=targets)

TypeError: object of type 'int' has no len()

While surprising, this is not unexpected. The docstring of Circuit.unitary says

...
Args:
    targets (QubitSet): Target qubits.
...

In alignment with this, the following code work without error:

import numpy as np
from braket.circuits import Circuit, QubitSet

target_qubit_indexes = (0,)
target_qubits = QubitSet(target_qubit_indexes)

circ0 = Circuit().unitary(matrix=np.array([[0, 1],[1, 0]]), targets=target_qubit_indexes)
circ1 = Circuit().unitary(matrix=np.array([[0, 1],[1, 0]]), targets=target_qubits)

I close this issue. If you think there is more to this problem than what I found, feel free to reopen and provide more information. Thanks.