5 N_Qubit_Decomposition_adaptive,
6 N_Qubit_Decomposition_custom,
7 N_Qubit_Decomposition_Tabu_Search,
8 N_Qubit_Decomposition_Tree_Search,
14 return np.eye(1 << n, dtype=dtype)
18 config = {
"use_float":
True}
20 N_Qubit_Decomposition,
21 N_Qubit_Decomposition_adaptive,
22 N_Qubit_Decomposition_custom,
23 N_Qubit_Decomposition_Tree_Search,
24 N_Qubit_Decomposition_Tabu_Search,
28 decomp = cls(
_identity(dtype=np.complex64), config=config)
29 assert decomp.get_Qbit_Num() == 2
34 params = np.array([], dtype=np.float64)
36 unitary = np.asarray(decomp.get_Unitary())
37 matrix = np.asarray(decomp.get_Matrix(params))
39 assert unitary.dtype == np.complex64
40 assert matrix.dtype == np.complex64
45 params = np.array([], dtype=np.float32)
47 unitary = np.asarray(decomp.get_Unitary())
48 matrix = np.asarray(decomp.get_Matrix(params))
50 assert unitary.dtype == np.complex64
51 assert matrix.dtype == np.complex64
52 np.testing.assert_allclose(matrix, np.eye(4, dtype=np.complex64), atol=1e-6)
57 replacement =
_identity(dtype=np.complex64)
59 assert decomp.set_Unitary(replacement) == 0
60 unitary = np.asarray(decomp.get_Unitary())
62 assert unitary.dtype == np.complex64
63 np.testing.assert_allclose(unitary, replacement, atol=1e-6)
68 params = np.array([], dtype=np.float32)
69 batch = np.zeros((2, 0), dtype=np.float32)
71 assert np.isclose(decomp.Optimization_Problem(params), 0.0, atol=1e-6)
73 grad = np.asarray(decomp.Optimization_Problem_Grad(params))
74 assert grad.dtype == np.float32
77 cost, combined_grad = decomp.Optimization_Problem_Combined(params)
78 assert np.isclose(cost, 0.0, atol=1e-6)
79 assert np.asarray(combined_grad).dtype == np.float32
81 batched = np.asarray(decomp.Optimization_Problem_Batch(batch))
82 assert batched.dtype == np.float32
83 np.testing.assert_allclose(batched, np.zeros(2, dtype=np.float32), atol=1e-6)
88 inner = Circuit(qbit_num)
90 for qbit
in range(qbit_num):
92 parameters.extend([0.1, 0.2, 0.3])
94 for qbit
in range(qbit_num):
96 parameters.extend([0.1, 0.2, 0.3])
98 outer = Circuit(qbit_num)
99 outer.add_Circuit(inner)
100 parameters = np.asarray(parameters, dtype=np.float32)
104 config={
"use_float":
True,
"parallel": 0},
106 decomp.set_Gate_Structure(outer)
109 cost, grad = decomp.Optimization_Problem_Combined(parameters)
110 assert np.isfinite(cost)
111 assert np.asarray(grad).dtype == np.float32
116 inner = Circuit(qbit_num)
124 outer = Circuit(qbit_num)
126 outer.add_Circuit(inner)
129 parameters = np.linspace(0.1, 1.7, 18, dtype=np.float64)
131 _identity(qbit_num, dtype=np.complex128),
132 config={
"parallel": 0},
134 decomp.set_Gate_Structure(outer)
136 _, grad = decomp.Optimization_Problem_Combined(parameters)
137 grad = np.asarray(grad)
140 finite_diff = np.empty_like(parameters)
141 for idx
in range(parameters.size):
142 params_plus = parameters.copy()
143 params_minus = parameters.copy()
144 params_plus[idx] += eps
145 params_minus[idx] -= eps
147 decomp.Optimization_Problem(params_plus)
148 - decomp.Optimization_Problem(params_minus)
151 np.testing.assert_allclose(grad, finite_diff, rtol=1e-5, atol=1e-6)
def test_nested_gate_block_combined_gradient_matches_finite_difference()
def _identity(n=2, dtype=np.complex64)
def test_float32_parameters_work_for_cost_grad_and_batch()
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...
def test_set_unitary_accepts_complex64_and_keeps_float_getter()
def test_decomposition_wrappers_accept_complex64_with_use_float()
def test_use_float_returns_float32_unitary_and_matrix()
def test_float32_combined_handles_nested_gate_blocks_repeatedly()
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...
def test_use_float_routes_complex128_input_to_float_path()