def hadamard_test_imag(): print("== expectaion value prediction (imaginary part) == ") # prepare initial state qs_0 = QState(1) qs_psi = QState(2).h(0).h(1) qs = qs_0.tenspro(qs_psi) # circuit for hadamard test qs.h(0).s(0) qs.ch(0, 1).crx(0, 2, phase=0.25) qs.h(0) shots = 1000 md = qs.m([0], shots=shots) p0 = md.frq[0] / shots p1 = md.frq[1] / shots exp_pred = p1 - p0 print("expectation value (predict) = {0:.3f}".format(exp_pred)) # theoretical expactation value qs_op = qs_psi.clone() qs_op.h(0).rx(1, phase=0.25) exp_theo = qs_psi.inpro(qs_op).imag print("expectation value (theoretical) = {0:.3f}".format(exp_theo))
def test_inpro_partial(self): """test 'inpro' (for partial system) """ qs_0 = QState(qubit_num=3).h(0).h(1).h(2) qs_1 = QState(qubit_num=3).h(0).h(1).h(2) inpro = qs_0.inpro(qs_1, qid=[0, 1]) ans = (round(inpro.real, 4) == 1.0 and inpro.imag == 0.0) self.assertEqual(ans, True)
def test_inpro(self): """test 'inpro' """ qs_0 = QState(qubit_num=3).h(0).h(1).h(2) qs_1 = QState(qubit_num=3).h(0).h(1).h(2) inpro = qs_0.inpro(qs_1) ans = (round(inpro.real, 4) == 1.0 and inpro.imag == 0.0) self.assertEqual(ans, True)