python-3.xcmathqiskitqubit

Quantum: Define superposition as linear combination of orthonormal basis


I'm testing a code for define a superposition state using a percentage of both basis.

All seems to run well, when the linear percentage is real. But it fails when the percentage is complex parameter.

from cmath import sqrt
from typing import List, Tuple

# Standard basis |0>, |1> (Z axis)
zero_qubit = np.matrix([[1.+0.j], [0.+0.j]])
one_qubit = np.matrix([[0.+0.j], [1.+0.j]])
standard_basis: Tuple[np.matrix] = (zero_qubit, one_qubit)


def superposition(
        percentage: float = 100.0,
        basis: Tuple[np.matrix] = (zero_qubit, one_qubit)):
    
    p=sqrt(percentage/100)
    q=sqrt(1-p*p)

    q1 = p*basis[0]+q*basis[1]
    q2 = p*basis[0]-q*basis[1]
    q3 = -p*basis[0]+q*basis[1]
    q4 = -p*basis[0]-q*basis[1]
    return q1, q2, q3, q4

# This works
q1,q2,q3,q4 = superposition(50,standard_basis)

print(q1)
print(q2)
print(q3)
print(q4)

# This generate weird values
q1,q2,q3,q4 = superposition(50j,standard_basis)

print(q1)
print(q2)
print(q3)
print(q4)

Solution

  • Answering my own question.

    The superposition has to consider modules of coeficients (not the coeficient themselves).

    So, my current code looks like

    from math import sqrt
    from typing import List, Tuple
    
    # https://en.wikipedia.org/wiki/Quantum_superposition
    def superposition(
            alfa: complex = sqrt(.5)+0.j,
            basis: Tuple[np.matrix] = (zero_qubit, one_qubit)):
        
        assert abs(alfa)<=1.0,"Module of assertion coef has to be lower or equal 1"
    
        if abs(alfa) >= 0.9999999:
            beta: complex = 0.+0.j
        elif abs(alfa) <=0.0000001:
            beta: complex = 1.+0.j
        else:
            alfa_module=abs(alfa)
            beta_module=sqrt(1-alfa_module**2)
            beta: complex=(beta_module/alfa_module)*(alfa.conjugate())
            assert abs(abs(beta)-beta_module)<0.0000001,"Internal error calculating modules"
    
        q1 = alfa*basis[0]+beta*basis[1]
        q2 = alfa*basis[0]+(-beta)*basis[1]
        q3 = (-alfa)*basis[0]+beta*basis[1]
        q4 = (-alfa)*basis[0]+(-beta)*basis[1]
        print(get_bloch_coordinates(q1))
        print(get_bloch_coordinates(q2))
        print(get_bloch_coordinates(q3))
        print(get_bloch_coordinates(q4))
        return q1, q2, q3, q4