def run(n, mappings): deutsch_program = pq.Program() qubits = [deutsch_program.alloc() for _ in range(n)] ancilla = deutsch_program.alloc() unitary_funct = unitary_function(mappings) oracle = oracle_function(unitary_funct, qubits, ancilla) deutsch_program += deutsch_jozsa(oracle, qubits, ancilla) deutsch_program.out() qvm = SyncConnection() results = qvm.run_and_measure(deutsch_program, [q.index() for q in qubits]) return "balanced" if 1 in results[0] else "constant"
for i, qubit in enumerate(qubits): if bitstring[i] == '0': prog.inst(X(qubit)) prog += amp.n_qubit_control(qubits[:-1], qubits[-1], np.array([[1, 0], [0, -1]]), 'Z') for i, qubit in enumerate(qubits): if bitstring[i] == '0': prog.inst(X(qubit)) return prog if __name__ == "__main__": from pyquil.api import SyncConnection import sys try: target = sys.argv[1] except IndexError: raise ValueError("Enter a target bitstring for Grover's Algorithm.") grover_program = pq.Program() qubits = range(len(target)) oracle = basis_selector_oracle(target, qubits) grover_program += grover(oracle, qubits) cxn = SyncConnection() mem = cxn.run_and_measure(grover_program, qubits) print(mem)
return np.allclose(np.eye(rows), mat.dot(mat.T.conj())) if __name__ == "__main__": from pyquil.api import SyncConnection # Read function mappings from user n = int(input("How many bits? ")) assert n > 0, "The number of bits must be positive." print("Enter f(x) for the following n-bit inputs:") mappings = [] for i in range(2**n): val = int(input(integer_to_bitstring(i, n) + ': ')) assert val in [0, 1], "f(x) must return only 0 and 1" mappings.append(val) deutsch_program = pq.Program() qubits = [deutsch_program.alloc() for _ in range(n)] ancilla = deutsch_program.alloc() unitary_funct = unitary_function(mappings) oracle = oracle_function(unitary_funct, qubits, ancilla) deutsch_program += deutsch_jozsa(oracle, qubits, ancilla) deutsch_program.out() print(deutsch_program) qvm = SyncConnection() results = qvm.run_and_measure(deutsch_program, [q.index() for q in qubits]) print("Results:", results) print("f(x) is", "balanced" if 1 in results[0] else "constant")
prog += n_qubit_control(qubits, query_qubit, np.array([[0, 1], [1, 0]]), 'NOT') for i, qubit in enumerate(qubits): if bitstring[i] == '0': prog.inst(X(qubit)) return prog if __name__ == "__main__": from pyquil.api import SyncConnection import sys try: target = sys.argv[1] except IndexError: raise ValueError("Enter a target bitstring for Grover's Algorithm.") grover_program = pq.Program() qubits = [grover_program.alloc() for _ in target] query_qubit = grover_program.alloc() oracle = basis_selector_oracle(target, qubits, query_qubit) grover_program += grover(oracle, qubits, query_qubit) # To instantiate the qubits in the program. This is just a temporary hack. grover_program.out() print grover_program cxn = SyncConnection() mem = cxn.run_and_measure(grover_program, [q.index() for q in qubits]) print mem