Skip to content

Commit

Permalink
Merge d7adb86 into 3d5d6b7
Browse files Browse the repository at this point in the history
  • Loading branch information
purva-thakre committed Jun 12, 2021
2 parents 3d5d6b7 + d7adb86 commit 5abdcff
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/qutip_qip/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def add_measurement(self, measurement, targets=None, index=None,
Parameters
----------
name: string
Measurement name. If name is an instance of `Measuremnent`,
Measurement name. If name is an instance of `Measurement`,
parameters are unpacked and added.
targets: list
Gate targets
Expand Down
33 changes: 33 additions & 0 deletions src/qutip_qip/decompositions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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 source 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.
###############################################################################
from qutip_qip.decompositions.general_decompositions import normalize_matrix, check_unitary
34 changes: 34 additions & 0 deletions src/qutip_qip/decompositions/converters.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 convert from numpy array to Qobj or Gate class object.
97 changes: 97 additions & 0 deletions src/qutip_qip/decompositions/general_decompositions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# 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.
###############################################################################

import numpy as np

def normalize_matrix(input_array)-> np.array:
""" Checks if the input gate's array is normalized or not. If not, makes
sure the input has been normalized.
This function will also check if the input is a valid array and a valid
quantum gate.
Args:
input_array : Matrix of a gate in Numpy array form.
"""
if not isinstance(input_array, np.ndarray):
raise TypeError("Not a valid input : A Numpy input array must be provided.")


if isinstance(input_array, np.ndarray):
if input_array.size==0:
raise ValueError("An empty array was provided as input.")

input_matrix_rows = input_array.shape[0]
input_matrix_columns = input_array.shape[1]
if input_matrix_rows !=input_matrix_columns:
raise ValueError("Input must be a square matrix to be a valid gate.")

if np.linalg.det(input_array) != 1:
if np.linalg.det(input_array) ==0:
raise ZeroDivisionError("Determinant of matrix =0.")
norm_factor = float(1/np.abs(np.linalg.det(input_array)))**0.5
input_array = np.around(norm_factor*input_array,5)
else:
input_array = input_array

return(input_array)

# Note this function is defined for qobj, re-defined here for a numpy array.
# Accessing individual elements of Qobj array could be problematic.
def check_unitary(input_array)-> bool:
"""Checks if the input matrix is unitary or not.
Args:
input_array : Matrix of a gate in Numpy array form.
"""
input_array = normalize_matrix(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))
check_unitary_right = np.allclose(identity_matrix, np.dot(input_array,input_array_dagger))
if check_unitary_left != check_unitary_right:
raise ArithmeticError("Unitary product assertions do not match.")
check_unitary = check_unitary_left
return(check_unitary)

def extract_global_phase(input_array):
"""Express input array as a product of some global phase factor and a special
unitary matrix array (returned in the form of a list containing `phasegate`
and some other special unitary array).
Args:
input_array : Matrix of a gate in Numpy array form.
"""
if check_unitary == True:
input_array = normalize_matrix(input_array)

32 changes: 32 additions & 0 deletions src/qutip_qip/decompositions/single_qubit_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.
###############################################################################
77 changes: 77 additions & 0 deletions tests/test_general_decompositions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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.
###############################################################################
import numpy as np
import pytest
from qutip_qip.decompositions.general_decompositions import normalize_matrix, check_unitary
from qutip import Qobj

def test_normalize_matrix_valid_input_not_unitary():
""" Test output when a valid input is provided which is not a unitary.
"""
normalized_M = normalize_matrix(np.array([[1, 2], [3, 4]]))
calculated_M = np.array([[0.70711, 1.41421],[2.12132, 2.82843]])
assert np.array_equal(calculated_M, normalized_M)
assert check_unitary(calculated_M) == False

@pytest.mark.parametrize("invalid_input", (Qobj([[1],[2],[3],[4],[5]]),(1,2)))
def test_normalize_matrix_invalid_input(invalid_input):
""" Test error when Qobj array is provided as input.
"""
with pytest.raises(TypeError, match="Not a valid input : A Numpy input array must be provided."):
normalize_matrix(invalid_input)

def test_normalize_matrix_empty_array():
"""When an empty array is provided as input."""
with pytest.raises(ValueError, match="An empty array was provided as input."):
normalize_matrix(np.array([]))


def test_normalize_matrix_non_square_matrix():
"""Test error is raised when row number and column number of Matrix
are mismatched."""
with pytest.raises(ValueError, match="Input must be a square matrix to be a valid gate."):
normalize_matrix(np.array([[1,4,3]]))
normalize_matrix(np.array([[1]]))
normalize_matrix(np.array([[1, 2], [3, 4],[5,6]]))

def test_normalize_matrix_zero_determinant():
"""Check if function tries to divide by 0 norm factor.
"""
with pytest.raises(ZeroDivisionError, match="Determinant of matrix =0."):
normalize_matrix(np.array([[0, 0], [0, 0]]))


def test_check_unitary():
"""Tests if input is correctly idenitified as unitary. """
input_array = np.array([[1+1j,1-1j],[1-1j,1+1j]])
assert check_unitary(input_array) == True
32 changes: 32 additions & 0 deletions tests/test_single_qubit_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 5abdcff

Please sign in to comment.