def test_inheritance(self): """test 'inheritance' """ bk = Backend('qlazy_qstate_simulator') qc = MyQComp(backend=bk, qubit_num=2, cmem_num=3) res = qc.bell(0, 1).measure(qid=[0, 1]).run(shots=10) qc.free() self.assertEqual(res['measured_qid'], [0, 1]) self.assertEqual(res['frequency']['00'] + res['frequency']['11'], 10)
def test_measure_simple(self): """test 'm' (simple case) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).cx(0, 1).measure([0, 1]).run(shots=10) qc.free() self.assertEqual(res['measured_qid'], [0, 1]) self.assertEqual(res['frequency']['00'] + res['frequency']['11'], 10)
def test_init_qlazy_stabilizer_simulator(self): """test '__init__' (qlazy_stabilizer_simulator) """ bk = Backend('qlazy_stabilizer_simulator') qc = QComp(qubit_num=3, cmem_num=2, backend=bk) actual = qc.stab.get_str() expect = " ZII\n IZI\n IIZ\n" qc.free() self.assertEqual(actual, expect)
def test_h_z(self): """test 'z' gate (following 'h' gate) """ bk = Backend('qlazy_stabilizer_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.h(0).z(0).run(reset_qubits=False) actual = qc.stab.get_str() expect = " -X\n" self.assertEqual(actual, expect) qc.free()
def test_cz(self): """test 'CZ' """ bk = Backend('qlazy_stabilizer_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.cz(0, 1).run(reset_qubits=False) actual = qc.stab.get_str() expect = " ZI\n IZ\n" qc.free() self.assertEqual(actual, expect)
def test_hh_cz(self): """test 'CZ' (folowint 'h' gate) """ bk = Backend('qlazy_stabilizer_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).h(1).cz(0, 1).run(reset_qubits=False) actual = qc.stab.get_str() expect = " XZ\n ZX\n" qc.free() self.assertEqual(actual, expect)
def test_measure_control_qubit(self): """test 'm' (control qubit using classical memory) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, cmem_num=3, backend=bk) res = qc.h(0).cx(0, 1).measure([0], [0]).x(0, ctrl=0).x( 1, ctrl=0).measure([0, 1]).run(shots=10) qc.free() self.assertEqual(res['measured_qid'], [0, 1]) self.assertEqual(res['frequency']['00'], 10)
def test_init_qlazy_qstate_simulator(self): """test '__init__' (qlazy_qstate_simulator) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=3, cmem_num=2, backend=bk) actual = qc.qstate.amp expect = np.array([1j, 0j, 0j, 0j, 0j, 0j, 0j, 0j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_y(self): """test 'y' gate """ bk = Backend('qlazy_stabilizer_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.y(0).run(reset_qubits=False) actual = qc.stab.get_str() expect = " -Z\n" self.assertEqual(actual, expect) qc.free()
def test_sw(self): """test 'sw' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).h(1).sw(0, 1).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.5 + 0j), (0.5 + 0j)]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_u3(self): """test 'u3' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.u3(0, alpha=0.1, beta=0.2, gamma=0.3).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0.89100652 + 0.j, 0.36728603 + 0.26684892j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_h_u2(self): """test 'u2' gate (following 'h' gate) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.h(0).u2(0, alpha=0.1, beta=0.2).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0.02447174 - 0.1545085j, 0.69840112 + 0.69840112j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_sw(self): """test 'sw' gate (following 'x' gate, not 'h' gates) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.x(0).sw(0, 1).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0j, (1 + 0j), 0j, 0j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_h_p(self): """test 'p' gate (following 'h' gate) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.h(0).p(0, phase=0.25).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0.70710678, 0.5 + 0.5j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_measure_use_cmem(self): """test 'm' (use classical memory) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, cmem_num=3, backend=bk) res = qc.h(0).cx(0, 1).measure([0, 1], [0, 1]).run(shots=10, reset_cmem=False) self.assertEqual(res['measured_qid'], [0, 1]) self.assertEqual(res['frequency']['00'] + res['frequency']['11'], 10) self.assertEqual(qc.cmem == [0, 0, 0] or qc.cmem == [1, 1, 0], True) qc.free()
def test_h_u1(self): """test 'u1' gate (following 'h' gate) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.h(0).u1(0, alpha=0.1).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0.70710678 + 0.j, 0.67249851 + 0.21850801j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_ry(self): """test 'ry' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.ry(0, phase=0.25).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([COS_PI_8, SIN_PI_8]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_xr_dg(self): """test 'xr_dg' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.xr_dg(0).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0.5 - 0.5j, 0.5 + 0.5j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_x_x_csw(self): """test 'csw' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=3, backend=bk) res = qc.x(0).x(1).csw(0, 1, 2).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0j, 0j, 0j, 0j, 0j, (1 + 0j), 0j, 0j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_reset_qlazy_stabilizer_simulator(self): """test 'reset' (qlazy_stabilizer_simulator) """ bk = Backend('qlazy_stabilizer_simulator') qc = QComp(qubit_num=3, cmem_num=2, backend=bk) qc.h(0).h(1).h(2).run() qc.reset() actual = qc.stab.get_str() expect = " ZII\n IZI\n IIZ\n" qc.free() self.assertEqual(actual, expect)
def test_u1(self): """test 'u1' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.u1(0, alpha=0.1).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([1.0, 0.0]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_u2(self): """test 'u2' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.u2(0, alpha=0.1, beta=0.2).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0.70710678 + 0.j, 0.5720614 + 0.41562694j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_h_t_dg(self): """test 't_dg' gate (following 'h' gate) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.h(0).t_dg(0).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([1.0 / SQRT_2, 0.5 - 0.5j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_h_u3(self): """test 'u3' gate (following 'h' gate) """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=1, backend=bk) res = qc.h(0).u3(0, alpha=0.1, beta=0.2, gamma=0.3).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([0.32472882 - 0.09920056j, 0.63003676 + 0.69840112j]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_cp(self): """test 'cp'gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).h(1).cp(0, 1, phase=0.25).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.5 + 0j), (0.3535533905932738 + 0.35355339059327373j)]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_cry(self): """test 'cry' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).h(1).cry(0, 1, phase=0.25).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.2705980500730985 + 0j), (0.6532814824381882 + 0j)]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_cu1(self): """test 'cu1' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).h(1).cu1(0, 1, alpha=0.1).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.5 + 0j), (0.47552825814757677 + 0.1545084971874737j)]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_crz(self): """test 'crz' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).h(1).crz(0, 1, phase=0.25).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.4619397662556434 - 0.1913417161825449j), (0.4619397662556434 + 0.1913417161825449j)]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_cu2(self): """test 'cu2' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).h(1).cu2(0, 1, alpha=0.1, beta=0.2).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.0173041346112951 - 0.10925400611220525j), (0.49384417029756883 + 0.49384417029756883j)]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)
def test_cu3(self): """test 'cu3' gate """ bk = Backend('qlazy_qstate_simulator') qc = QComp(qubit_num=2, backend=bk) res = qc.h(0).h(1).cu3(0, 1, alpha=0.1, beta=0.2, gamma=0.3).run(reset_qubits=False) actual = qc.qstate.amp expect = np.array([(0.5 + 0j), (0.5 + 0j), (0.22961795053748937 - 0.07014538985214754j), (0.44550326209418395 + 0.4938441702975689j)]) ans = equal_vectors(actual, expect) qc.free() self.assertEqual(ans, True)