Skip to content

Commit

Permalink
real determinant
Browse files Browse the repository at this point in the history
  • Loading branch information
purva-thakre committed Jun 13, 2021
1 parent 0a7f660 commit a7efe07
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/qutip_qip/decompositions/general_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ def check_unitary(input_array)-> bool:
Args:
input_array : Matrix of a gate in Numpy array form.
"""
input_array = normalize_matrix(input_array)
try:
input_array = normalize_matrix(input_array)
except ZeroDivisionError:
input_array = input_array
identity_matrix = np.eye(input_array.shape[0])
input_array_dagger = input_array.conj().T
check_unitary_left = np.allclose(identity_matrix, np.dot(input_array_dagger,input_array))
Expand All @@ -96,9 +99,13 @@ def extract_global_phase(input_array):
input_array : Matrix of a gate in Numpy array form.
"""
if check_unitary(input_array) is True:
input_array = normalize_matrix(input_array)
power_factor_based_on_matrix_shape = 1/input_array.shape[0]
try:
input_array = normalize_matrix(input_array)
except ZeroDivisionError:
input_array = input_array

det = np.linalg.det(input_array)
power_factor_based_on_matrix_shape = 1/input_array.shape[0]


sin_value_from_determinant = -det.imag
Expand All @@ -114,12 +121,11 @@ def extract_global_phase(input_array):
phase_factor = np.arctan(ratio_sin_cos)

phase_factor = power_factor_based_on_matrix_shape*phase_factor
phase_exp_value = np.cos(phase_factor) - 1j*np.sin(phase_factor)


if phase_factor == -0.0:
phase_exp_value = -1
else:
phase_exp_value = np.cos(phase_factor) - 1j*np.sin(phase_factor)
# Makes sure the value at |0><0| is positive (not really needed)
if phase_exp_value > 0 and input_array[0][0] <0:
phase_exp_value = - phase_exp_value

input_to_su_from_phase = np.multiply(input_array,phase_exp_value)
return((phase_exp_value,input_to_su_from_phase))
Expand Down
34 changes: 34 additions & 0 deletions src/qutip_qip/decompositions/verify_decompositions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This file is part of QuTiP: Quantum Toolbox in Python.
#
# Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
# All rights reserved.
#
# Redistribution and use in sourc e and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
# of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################

# add functions to check the calculated decomposition is equivalent to input array.
39 changes: 39 additions & 0 deletions tests/test_general_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ def test_normalize_matrix_zero_determinant():
with pytest.raises(ZeroDivisionError, match="Determinant of matrix =0."):
normalize_matrix(np.array([[0, 0], [0, 0]]))

def test_normalize_matrix_normalized_array():
"""Check if function tries to divide by 0 norm factor.
"""
pauliz = np.array([[1,0],[0,-1]])
calculated_array = normalize_matrix(pauliz)
assert all([out==inp] for out, inp in zip(calculated_array,pauliz))


def test_check_unitary():
"""Tests if input is correctly idenitified as unitary. """
Expand All @@ -89,3 +96,35 @@ def test_extract_global_phase_non_unitary_input():
a unitary."""
with pytest.raises(ValueError, match = "Input array is not a unitary matrix."):
extract_global_phase(np.array([[1, 2], [3, 4]]))

def test_extract_global_phase_valid_input_phase_comparison():
"""Tests if phase is correctly calculated when input is valid. """

# Determinant has real part only
matrix1 = np.multiply(np.array([[1,1],[1,-1]]),1/np.sqrt(2))
determined_list_of_gates = extract_global_phase(matrix1)
determined_phase_value = determined_list_of_gates[0]
actual_phase_value = 1+0j
assert(determined_phase_value==actual_phase_value)


def test_extract_global_phase_valid_input_output_comparison():
"""Tests if product of outputs is equal to input when input is valid. """

# Determinant has real part only
matrix1 = np.multiply(np.array([[1,1],[1,-1]]),1/np.sqrt(2))
determined_list_of_gates = extract_global_phase(matrix1)
determined_phase_value = determined_list_of_gates[0]
calculated_array = np.multiply(determined_list_of_gates[1],determined_phase_value)
assert all([out==inp] for out, inp in zip(calculated_array,matrix1))


def test_extract_global_phase_valid_input_shape_comparison():
"""Tests if shape of array is unchanged by function. """

# Determinant has real part only
matrix1 = np.multiply(np.array([[1,1],[1,-1]]),1/np.sqrt(2))
determined_list_of_gates = extract_global_phase(matrix1)
determined_array_shape = np.shape(determined_list_of_gates[1])
input_array_shape = np.shape(matrix1)
assert all([out==inp] for out, inp in zip(input_array_shape,determined_array_shape))
32 changes: 32 additions & 0 deletions tests/test_verify_decompositions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file is part of QuTiP: Quantum Toolbox in Python.
#
# Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
# All rights reserved.
#
# Redistribution and use in sourc e and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
# of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################

0 comments on commit a7efe07

Please sign in to comment.