def test_Ph_eigenvectors(): rule_set = DecompositionRuleSet(modules=[pe, dqft]) eng = MainEngine(backend=Simulator(), engine_list=[ AutoReplacer(rule_set), ]) results = np.array([]) for i in range(100): autovector = eng.allocate_qureg(1) theta = cmath.pi * 2. * 0.125 unit = Ph(theta) ancillas = eng.allocate_qureg(3) QPE(unit) | (ancillas, autovector) All(Measure) | ancillas fasebinlist = [int(q) for q in ancillas] fasebin = ''.join(str(j) for j in fasebinlist) faseint = int(fasebin, 2) phase = faseint / (2.**(len(ancillas))) results = np.append(results, phase) All(Measure) | autovector eng.flush() num_phase = (results == 0.125).sum() assert num_phase / 100. >= 0.35, "Statistics phase calculation are not correct (%f vs. %f)" % ( num_phase / 100., 0.35)
def test_simple_test_X_eigenvectors(): rule_set = DecompositionRuleSet(modules=[pe, dqft]) eng = MainEngine( backend=Simulator(), engine_list=[ AutoReplacer(rule_set), ], ) N = 150 results = np.array([]) for i in range(N): autovector = eng.allocate_qureg(1) X | autovector H | autovector unit = X ancillas = eng.allocate_qureg(1) QPE(unit) | (ancillas, autovector) All(Measure) | ancillas fasebinlist = [int(q) for q in ancillas] fasebin = ''.join(str(j) for j in fasebinlist) faseint = int(fasebin, 2) phase = faseint / (2.0**(len(ancillas))) results = np.append(results, phase) All(Measure) | autovector eng.flush() num_phase = (results == 0.5).sum() assert num_phase / N >= 0.35, "Statistics phase calculation are not correct (%f vs. %f)" % ( num_phase / N, 0.35, )
def test_X_no_eigenvectors(): rule_set = DecompositionRuleSet( modules=[pe, dqft, stateprep2cnot, ucr2cnot]) eng = MainEngine( backend=Simulator(), engine_list=[ AutoReplacer(rule_set), ], ) N = 100 results = np.array([]) results_plus = np.array([]) results_minus = np.array([]) for i in range(N): autovector = eng.allocate_qureg(1) amplitude0 = (np.sqrt(2) + np.sqrt(6)) / 4.0 amplitude1 = (np.sqrt(2) - np.sqrt(6)) / 4.0 StatePreparation([amplitude0, amplitude1]) | autovector unit = X ancillas = eng.allocate_qureg(1) QPE(unit) | (ancillas, autovector) All(Measure) | ancillas fasebinlist = [int(q) for q in ancillas] fasebin = ''.join(str(j) for j in fasebinlist) faseint = int(fasebin, 2) phase = faseint / (2.0**(len(ancillas))) results = np.append(results, phase) Tensor(H) | autovector if np.allclose(phase, 0.0, rtol=1e-1): results_plus = np.append(results_plus, phase) All(Measure) | autovector autovector_result = int(autovector) assert autovector_result == 0 elif np.allclose(phase, 0.5, rtol=1e-1): results_minus = np.append(results_minus, phase) All(Measure) | autovector autovector_result = int(autovector) assert autovector_result == 1 eng.flush() total = len(results_plus) + len(results_minus) plus_probability = len(results_plus) / N assert total == pytest.approx(N, abs=5) assert plus_probability == pytest.approx( 1.0 / 4.0, abs=1e-1 ), "Statistics on |+> probability are not correct (%f vs. %f)" % ( plus_probability, 1.0 / 4.0, )
def test_string(): unit = X gate = QPE(unit) assert (str(gate) == "QPE(X)")