PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.17k stars 568 forks source link

[BUG] Calculating the matrix of a broadcasted `GlobalPhase` with wires #5880

Open albi3ro opened 1 week ago

albi3ro commented 1 week ago

Expected behavior

I would expect to get a matrix of size (3,2,2).

Actual behavior

traceback below

Additional information

No response

Source code

op = qml.GlobalPhase((1.0, 2.0, 3.0), wires=0)
op.matrix()

Tracebacks

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[13], line 2
      1 op = qml.GlobalPhase((1.0, 2.0, 3.0), wires=0)
----> 2 op.matrix()

File /pennylane/pennylane/ops/identity.py:408, in GlobalPhase.matrix(self, wire_order)
    406 def matrix(self, wire_order=None):
    407     n_wires = len(wire_order) if wire_order else len(self.wires)
--> 408     return self.compute_matrix(self.data[0], n_wires=n_wires)

File /pennylane/pennylane/ops/identity.py:338, in GlobalPhase.compute_matrix(phi, n_wires)
    336 if interface == "tensorflow":
    337     return qml.math.exp(-1j * qml.math.cast(phi, complex)) * qml.math.eye(int(2**n_wires))
--> 338 return qml.math.exp(-1j * qml.math.cast(phi, complex)) * qml.math.eye(
    339     int(2**n_wires), like=interface
    340 )

ValueError: operands could not be broadcast together with shapes (3,) (2,2)

System information

pl master

Existing GitHub issues