def test_phase_invalid_function(): main_engine = MainEngine(backend=DummyEngine(), engine_list=[DummyEngine()]) qureg = main_engine.allocate_qureg(3) with pytest.raises(AttributeError): PhaseOracle(-42) | qureg with pytest.raises(AttributeError): PhaseOracle(0xcafe) | qureg with pytest.raises(RuntimeError): PhaseOracle(0x8e, synth=lambda: revkit.esopbs()) | qureg
def test_phase_majority(): sim = Simulator() main_engine = MainEngine(sim) qureg = main_engine.allocate_qureg(3) All(H) | qureg PhaseOracle(0xe8) | qureg main_engine.flush() assert np.array_equal(np.sign(sim.cheat()[1]), [1., 1., 1., -1., 1., -1., -1., -1.]) All(Measure) | qureg
def test_phase_majority_from_python(): dormouse = pytest.importorskip('dormouse') def maj(a, b, c): return (a and b) or (a and c) or (b and c) # pragma: no cover sim = Simulator() main_engine = MainEngine(sim) qureg = main_engine.allocate_qureg(3) All(H) | qureg PhaseOracle(maj) | qureg main_engine.flush() assert np.array_equal(np.sign(sim.cheat()[1]), [1., 1., 1., -1., 1., -1., -1., -1.]) All(Measure) | qureg
# permutation pi = [0, 2, 3, 5, 7, 1, 4, 6] eng = MainEngine() qubits = eng.allocate_qureg(6) x = qubits[::2] # qubits on odd lines y = qubits[1::2] # qubits on even lines # circuit with Compute(eng): All(H) | qubits All(X) | [x[0], x[1]] PermutationOracle(pi) | y PhaseOracle(f) | qubits Uncompute(eng) with Compute(eng): with Dagger(eng): PermutationOracle(pi, synth=revkit.dbs) | x PhaseOracle(f) | qubits Uncompute(eng) All(H) | qubits All(Measure) | qubits # measurement result print("Shift is {}".format(sum(int(q) << i for i, q in enumerate(qubits))))