Qiskit / qiskit-metapackage

Qiskit is an open-source SDK for working with quantum computers at the level of circuits, algorithms, and application modules.
https://qiskit.org
Apache License 2.0
3.03k stars 750 forks source link

coef times MatrixOp done wrong sometimes #1695

Closed hadizadeh-ali closed 1 year ago

hadizadeh-ali commented 1 year ago
import numpy as np
from qiskit.opflow import I, MatrixOp

def positional(n: int, i: int, ketbra: str):
    KETBRA = np.eye(2)
    if ketbra[0] == '0':
        KETBRA = KETBRA @ np.array([[1, 0]]).T
    else:
        KETBRA = KETBRA @ np.array([[0, 1]]).T
    if ketbra[1] == '0':
        KETBRA = KETBRA @ np.array([[1, 0]])
    else:
        KETBRA = KETBRA @ np.array([[0, 1]])
    return (I ^ i) ^ MatrixOp(KETBRA) ^ (I ^ (n-i-1))

(3 * positional(2, 0, '11') @ positional(2, 1, '11')).to_matrix()

Output:

array([[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 9.+0.j]])

Correct output:

array([[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 3.+0.j]])