Prev Tutorial: KL Divergence
Next Tutorial: Matrix
Application
Unitary matrix of QCircuit
Obtain the unitary matrix of the quantum circuit QCircuit
Here is API doc
Python
cir = QCircuit()
cir << X(0)<<CP(2,1,5.28)<<CNOT(1,0)
matrix = Unitary(cir)
print(matrix)
Output
{
{
{(0,0)},{(1,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(1,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(1,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(1,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(1,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(1,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0.537619,-0.843188)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0.537619,-0.843188)},
}
}
Obtain internal data
Get numpy.ndarray
Get internal data, the result is returned as a numpy.ndarray object
Here is API doc for Unitary.ndarray
Python
def test_ndarray():
cir = QCircuit()
cir << X(0)<<CP(2,1,5.28)<<CNOT(1,0)
U = Unitary(cir)
print("U",U)
arr = U.ndarray()
print("arr:",arr)
if __name__ == "__main__":
test_ndarray()
Output
U {
{
{(0,0)},{(1,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(1,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(1,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(1,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(1,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(1,0)},{(0,0)},{(0,0)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0.537619,-0.843188)},{(0,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0,0)},{(0.537619,-0.843188)},
}
}
arr: [[0. +0.j 1. +0.j 0. +0.j
1. +0.j 0. +0.j 0. +0.j
2. +0.j 0. +0.j ]
[1. +0.j 0. +0.j 0. +0.j
1. +0.j 0. +0.j 0. +0.j
2. +0.j 0. +0.j ]
[0. +0.j 0. +0.j 1. +0.j
1. +0.j 0. +0.j 0. +0.j
2. +0.j 0. +0.j ]
[0. +0.j 0. +0.j 0. +0.j
1. +0.j 0. +0.j 0. +0.j
2. +0.j 0. +0.j ]
[0. +0.j 0. +0.j 0. +0.j
1. +0.j 0. +0.j 1. +0.j
2. +0.j 0. +0.j ]
[0. +0.j 0. +0.j 0. +0.j
1. +0.j 1. +0.j 0. +0.j
2. +0.j 0. +0.j ]
[0. +0.j 0. +0.j 0. +0.j
1. +0.j 0. +0.j 0. +0.j
0.53761923-0.84318774j 0. +0.j ]
[0. +0.j 0. +0.j 0. +0.j
1. +0.j 0. +0.j 0. +0.j
2. +0.j 0.53761923-0.84318774j]]
Equality comparison function
Determine whether the internal data of two Unitary objects are equal
Here is API doc
Python
def test_eq():
cir = QCircuit()
cir << X(0)<<CP(2,1,5.28)<<CNOT(1,0)
U = Unitary(cir)
U2 = U
print("U2==U:",U2==U)
return U2==U
if __name__ == "__main__":
test_eq()
Output