-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5e01da
commit 196be45
Showing
6 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import numpy as np | ||
import cmath | ||
import pytest | ||
|
||
from qutip.qobj import Qobj | ||
from qutip_qip.operations import * | ||
from qutip_qip.circuit import QubitCircuit, Gate | ||
|
||
from qutip_qip.decompositions.general_decompositions import (check_input, | ||
check_input_shape, convert_qobj_gate_to_array, extract_global_phase) | ||
|
||
from qutip_qip.decompositions.single_decompositions import (_ZYZ_rotation, _ZXZ_rotation, _rotation_matrices_dictionary, | ||
ABC_decomposition, decompose_to_rotation_matrices) | ||
|
||
from qutip_qip.decompositions.decompositions_extras import (decomposed_gates_to_circuit, matrix_of_decomposed_gates) | ||
|
||
|
||
# Tests for decomposed_gates_to_circuit | ||
@pytest.mark.parametrize("invalid_input",[np.array([[1,1],[1,1]]),([[1,1],[1,1]]),1.5,3]) | ||
def test_decomposed_gates_to_circuit(invalid_input): | ||
"""Checks if correct error is raised when input is anything but a tuple of gates. | ||
""" | ||
with pytest.raises(TypeError,match="Input is not a tuple of gates."): | ||
decomposed_gates_to_circuit(invalid_input,1) | ||
|
||
H = Qobj([[1/np.sqrt(2),1/np.sqrt(2)],[1/np.sqrt(2),-1/np.sqrt(2)]]) | ||
sigmax = Qobj([[0,1],[1,0]]) | ||
H_zyz_gates = _ZYZ_rotation(H, 1, 0) | ||
sigmax_zyz_gates = _ZYZ_rotation(sigmax, 1, 0) | ||
|
||
@pytest.mark.parametrize("valid_input",[H_zyz_gates,sigmax_zyz_gates]) | ||
def test_decomposed_gates_to_circuit(valid_input): | ||
"""Checks if output is of type QubitCircuit. | ||
""" | ||
assert(isinstance(decomposed_gates_to_circuit(valid_input,1),QubitCircuit)) | ||
|
||
|
||
H_zyz_quantum_circuit = decomposed_gates_to_circuit(H_zyz_gates, 1) | ||
sigmax_zyz_quantum_circuit = decomposed_gates_to_circuit(sigmax_zyz_gates, 1) | ||
sigmax_zyz_output = (sigmax_zyz_quantum_circuit) | ||
# Tests for matrix_of_decomposed_gates | ||
@pytest.mark.parametrize("invalid_input",[np.array([[1,1],[1,1]]),([[1,1],[1,1]]),1.5,3]) | ||
def test_matrix_of_decomposed_gates(invalid_input): | ||
"""Checks if correct error is raised when input is anything but a quantum circuit. | ||
""" | ||
with pytest.raises(TypeError,match="Input is not of type QubitCircuit."): | ||
matrix_of_decomposed_gates(invalid_input) | ||
|
||
|
||
@pytest.mark.parametrize("valid_input",[H_zyz_quantum_circuit,sigmax_zyz_quantum_circuit]) | ||
def test_matrix_of_decomposed_gates(valid_input): | ||
"""Checks if final output is a Qobj. | ||
""" | ||
final_output=matrix_of_decomposed_gates(valid_input) | ||
assert(isinstance(final_output, Qobj)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import numpy as np | ||
import cmath | ||
import pytest | ||
|
||
from qutip.qobj import Qobj | ||
from qutip.metrics import average_gate_fidelity | ||
|
||
|
||
from qutip_qip.decompositions.single_decompositions import (_ZYZ_rotation, _ZXZ_rotation, | ||
ABC_decomposition, decompose_to_rotation_matrices) | ||
|
||
from qutip_qip.decompositions.decompositions_extras import (decomposed_gates_to_circuit, matrix_of_decomposed_gates) | ||
|
||
# Fidelity closer to 1 means the two states are similar to each other | ||
H = Qobj([[1/np.sqrt(2),1/np.sqrt(2)],[1/np.sqrt(2),-1/np.sqrt(2)]]) | ||
sigmax = Qobj([[0,1],[1,0]]) | ||
sigmay = Qobj([[0,-1j],[1j,0]]) | ||
sigmaz = Qobj([[1,0],[0,-1]]) | ||
SQRTNOT = Qobj([[1/np.sqrt(2),-1j/np.sqrt(2)],[-1j/np.sqrt(2),1/np.sqrt(2)]]) | ||
T = Qobj([[1,0],[0,cmath.rect(1, np.pi/4)]]) | ||
S = Qobj([[1,0],[0,1j]]) | ||
|
||
@pytest.mark.parametrize("gate",[H, sigmax, sigmay, sigmaz, SQRTNOT, S, T]) | ||
@pytest.mark.parametrize("method",[_ZYZ_rotation, _ZXZ_rotation, ABC_decomposition]) | ||
def test_single_qubit_to_rotations(gate, method): | ||
"""Initial matrix and product of final decompositions are same within some phase.""" | ||
target = 0 | ||
gate_list = method(gate,1,target) | ||
decomposed_gates_circuit = decomposed_gates_to_circuit(gate_list,1) | ||
decomposed_gates_final_matrix = matrix_of_decomposed_gates(decomposed_gates_circuit) | ||
fidelity_of_input_output = average_gate_fidelity(gate, decomposed_gates_final_matrix) | ||
assert(np.isclose(fidelity_of_input_output,1.0)) | ||
|
||
@pytest.mark.parametrize("gate",[H, sigmax, sigmay, sigmaz, SQRTNOT, S, T]) | ||
@pytest.mark.parametrize("method",['ZXZ','ZYZ']) | ||
def test_check_single_qubit_to_decompose_to_rotations(gate, method): | ||
"""Initial matrix and product of final decompositions are same within some phase.""" | ||
target = 0 | ||
gate_list = decompose_to_rotation_matrices(gate,1,target,method) | ||
decomposed_gates_circuit = decomposed_gates_to_circuit(gate_list,1) | ||
decomposed_gates_final_matrix = matrix_of_decomposed_gates(decomposed_gates_circuit) | ||
fidelity_of_input_output = average_gate_fidelity(gate, decomposed_gates_final_matrix) | ||
assert(np.isclose(fidelity_of_input_output,1.0)) | ||
|
||
@pytest.mark.parametrize("gate",[H, sigmax, sigmay, sigmaz, SQRTNOT, S, T]) | ||
@pytest.mark.parametrize("method",[_ZYZ_rotation, _ZXZ_rotation, ABC_decomposition]) | ||
def test_output_is_tuple(gate, method): | ||
"""Initial matrix and product of final decompositions are same within some phase.""" | ||
target = 0 | ||
gate_list = method(gate,1,target) | ||
assert(isinstance(gate_list, tuple)) | ||
|
||
@pytest.mark.parametrize("gate",[H, sigmax, sigmay, sigmaz, SQRTNOT, S, T]) | ||
@pytest.mark.parametrize("method",['ZXZ','ZYZ']) | ||
def test_check_single_qubit_to_decompose_to_rotations(gate, method): | ||
"""Initial matrix and product of final decompositions are same within some phase.""" | ||
target = 0 | ||
gate_list = decompose_to_rotation_matrices(gate,1,target,method) | ||
assert(isinstance(gate_list, tuple)) |