Skip to content

Commit 70e4224

Browse files
author
Zhuoyang Ye
committed
[Test] Add three qubit gate tests for density matrix.
1 parent 4f90e92 commit 70e4224

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

test/density/test_density_op.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def density_is_close(mat1: np.ndarray, mat2: np.ndarray):
132132

133133

134134

135-
class single_qubit(TestCase):
135+
class single_qubit_test(TestCase):
136136
def compare_single_gate(self, gate_pair, qubit_num):
137137
passed = True
138138
for index in range(0, qubit_num):
@@ -157,7 +157,7 @@ def test_single_gates(self):
157157

158158

159159

160-
class Two_qubit(TestCase):
160+
class two_qubit_test(TestCase):
161161
def compare_two_qubit_gate(self, gate_pair, qubit_num):
162162
passed = True
163163
for index1 in range(0, qubit_num):
@@ -184,5 +184,34 @@ def test_two_qubits_gates(self):
184184
self.assertTrue(self.compare_two_qubit_gate(two_qubit_gate_list[i], 5))
185185

186186

187+
class three_qubit_test(TestCase):
188+
def compare_three_qubit_gate(self, gate_pair, qubit_num):
189+
passed = True
190+
for index1 in range(0, qubit_num):
191+
for index2 in range(0, qubit_num):
192+
if (index1 == index2):
193+
continue
194+
for index3 in range(0, qubit_num):
195+
if (index3 == index1) or (index3 == index2):
196+
continue
197+
qdev = tq.NoiseDevice(n_wires=qubit_num, bsz=1, device="cpu", record_op=True)
198+
gate_pair['tq'](qdev, [index1,index2,index3])
199+
mat1 = np.array(qdev.get_2d_matrix(0))
200+
rho_qiskit = qiskitDensity.from_label('0' * qubit_num)
201+
rho_qiskit = rho_qiskit.evolve(gate_pair['qiskit'](), [qubit_num - 1 - index1,qubit_num - 1 - index2,qubit_num - 1 - index3])
202+
mat2 = np.array(rho_qiskit.to_operator())
203+
if density_is_close(mat1, mat2):
204+
print("Test passed for %s gate on qubit (%d,%d,%d) when qubit_number is %d!" % (
205+
gate_pair['name'], index1,index2,index3, qubit_num))
206+
else:
207+
passed = False
208+
print("Test failed for %s gate on qubit (%d,%d,%d) when qubit_number is %d!" % (
209+
gate_pair['name'], index1,index2,index3,qubit_num))
210+
return passed
211+
212+
def test_three_qubits_gates(self):
213+
for i in range(0, len(three_qubit_gate_list)):
214+
self.assertTrue(self.compare_three_qubit_gate(three_qubit_gate_list[i], 5))
215+
187216

188217

0 commit comments

Comments
 (0)