def test_qft(): np.testing.assert_allclose(cirq.unitary( cirq.QFT(*cirq.LineQubit.range(2))), np.array([ [1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j], ]) / 2, atol=1e-8) np.testing.assert_allclose(cirq.unitary( cirq.QFT(*cirq.LineQubit.range(2), without_reverse=True)), np.array([ [1, 1, 1, 1], [1, -1, 1, -1], [1, 1j, -1, -1j], [1, -1j, -1, 1j], ]) / 2, atol=1e-8) np.testing.assert_allclose( cirq.unitary(cirq.QFT(*cirq.LineQubit.range(4))), np.array([[np.exp(2j * np.pi * i * j / 16) for i in range(16)] for j in range(16)]) / 4, atol=1e-8) np.testing.assert_allclose(cirq.unitary( cirq.QFT(*cirq.LineQubit.range(2))**-1), np.array([ [1, 1, 1, 1], [1, -1j, -1, 1j], [1, -1, 1, -1], [1, 1j, -1, -1j], ]) / 2, atol=1e-8) for k in range(4): for b in [False, True]: cirq.testing.assert_implements_consistent_protocols( cirq.QuantumFourierTransformGate(num_qubits=k, without_reverse=b))
def test_circuit_diagram(): cirq.testing.assert_has_diagram( cirq.Circuit.from_ops( cirq.decompose_once(cirq.QFT(*cirq.LineQubit.range(4)))), """ 0: ───H───Grad^0.5───────#2─────────────#3─────────────×─── │ │ │ │ 1: ───────@──────────H───Grad^0.5───────#2─────────×───┼─── │ │ │ │ 2: ──────────────────────@──────────H───Grad^0.5───×───┼─── │ │ 3: ─────────────────────────────────────@──────────H───×─── """) cirq.testing.assert_has_diagram( cirq.Circuit.from_ops( cirq.decompose_once( cirq.QFT(*cirq.LineQubit.range(4), without_reverse=True))), """ 0: ───H───Grad^0.5───────#2─────────────#3───────────── │ │ │ 1: ───────@──────────H───Grad^0.5───────#2───────────── │ │ 2: ──────────────────────@──────────H───Grad^0.5─────── │ 3: ─────────────────────────────────────@──────────H─── """) cirq.testing.assert_has_diagram( cirq.Circuit.from_ops(cirq.QFT(*cirq.LineQubit.range(4)), cirq.inverse( cirq.QFT(*cirq.LineQubit.range(4)))), """ 0: ───QFT───QFT^-1─── │ │ 1: ───#2────#2─────── │ │ 2: ───#3────#3─────── │ │ 3: ───#4────#4─────── """)
def QPEStatePreparation(theta, phi, qubits, n_ancilla,u, v, t, delta_t): ''' Function that implements the QPE to for state preparation. Parameters ---------- theta : float Angle to initialize the ansatz with. phi : float Angle to initialize the ansatz with. qubits : c.LineQubit The collection of qubits the algorithm acts on. ancilla_qubits : int The umber of ancilla's the algorithm uses. u : float One the parameters in the Hamiltonian, see report for description. v : float One the parameters in the Hamiltonian, see report for description. t : float One the parameters in the Hamiltonian, see report for description. delta_t : float The time period used in the unitary. Yields ------ c.Gates Yield multiple gates as implemented in cirq. ''' ancilla_qubits = [c.LineQubit(i) for i in range(4, 4 + n_ancilla)] yield StatePreparation(theta, phi)(*qubits) for i in range(0, n_ancilla ): yield c.H(ancilla_qubits[i]) for i in range(0, n_ancilla ): yield TE.TrotterizedEvolutionGate(u, v, t, delta_t * 2**i).controlled()(ancilla_qubits[i], *qubits) yield c.QFT(*ancilla_qubits, inverse = True) yield c.measure(*ancilla_qubits, key='ancilla')
def make_order_finding_circuit(x: int, n: int) -> cirq.Circuit: """Returns quantum circuit which computes the order of x modulo n. The circuit uses Quantum Phase Estimation to compute an eigenvalue of the unitary U|y⟩ = |y * x mod n⟩ 0 <= y < n U|y⟩ = |y⟩ n <= y discussed in the header of this file. The circuit uses two registers: the target register which is acted on by U and the exponent register from which an eigenvalue is read out after measurement at the end. The circuit consists of three steps: 1. Initialization of the target register to |0..01⟩ and the exponent register to a superposition state. 2. Multiple controlled-U**2**j operations implemented efficiently using modular exponentiation. 3. Inverse Quantum Fourier Transform to kick an eigenvalue to the exponent register. Args: x: positive integer whose order modulo n is to be found n: modulus relative to which the order of x is to be found Returns: Quantum circuit for finding the order of x modulo n """ L = n.bit_length() target = cirq.LineQubit.range(L) exponent = cirq.LineQubit.range(L, 3 * L + 3) return cirq.Circuit( cirq.X(target[L - 1]), cirq.H.on_each(*exponent), ModularExp(target, exponent, x, n), cirq.QFT(*exponent, inverse=True), cirq.measure(*exponent, key='exponent'), )
def _decompose_(self, qubits): qubits = list(qubits) yield cirq.H.on_each(*qubits[:-1]) yield PhaseKickback(self.num_qubits(), self.U)(*qubits) yield cirq.QFT(*qubits[:-1], without_reverse=True)**-1
def test_deprecated(): with cirq.testing.assert_logs('cirq.qft', 'deprecated'): _ = cirq.QFT(*cirq.LineQubit.range(3))
def test_inverse(): a, b, c = cirq.LineQubit.range(3) assert cirq.QFT(a, b, c, inverse=True) == cirq.QFT(a, b, c)**-1 assert cirq.QFT(a, b, c, inverse=True, without_reverse=True) == cirq.inverse( cirq.QFT(a, b, c, without_reverse=True))
# print(f'H A{i}') for j in range(i + 1, N): n = j - i + 1 CRn(qc, n, ctrl=A[j], targ=A[j - 1], ctrl_opp=B[j - 1], targ_opp=B[j]) # print(f'CR{n} A{j}, A{j-1}, B{j-1}, B{j}') if j != N - 1: qc.append(iswap_1.on(A[j - 1], A[j])) # print(f'iSWAPπ A{j-1} A{j}') for j in reversed(range(i + 1, N - 1)): # range(N-2, i+1) qc.append(iswap_2.on(A[j - 1], A[j])) # print(f'iSWAP3π/2 A{j-1} A{j}') post_qft_cross_swap(qc, A) # Do QFT dQFT(qc, A, B) sim_print(qc) print(qc.to_text_diagram(qubit_order=A + B)) ndtotext_print(qc.unitary(qubit_order=B + A)[:2**n_qft, :2**n_qft]) qc.append(cirq.QFT(*A)**(-1)) print('This should be the input encoding only!') ndtotext_print(qc.unitary(qubit_order=B + A)[:2**n_qft, :2**n_qft])
c.append(cirq.CSWAP(qubits[2], qubits[7], qubits[9])) c.append(cirq.CSWAP(qubits[2], qubits[6], qubits[8])) c.append(cirq.CCX(qubits[2], qubits[6], qubits[8])) c.append(cirq.CCX(qubits[2], qubits[8], qubits[9])) c.append(cirq.CCX(qubits[3], qubits[8], qubits[9])) c.append(cirq.CCX(qubits[6], qubits[3], qubits[8])) c.append(cirq.CSWAP(qubits[3], qubits[6], qubits[8])) c.append(cirq.CSWAP(qubits[3], qubits[7], qubits[9])) c.append(cirq.CSWAP(qubits[3], qubits[5], qubits[7])) c.append(cirq.CSWAP(qubits[4], qubits[5], qubits[7])) c.append(cirq.CSWAP(qubits[4], qubits[7], qubits[9])) c.append(cirq.CSWAP(qubits[4], qubits[6], qubits[8])) c.append(cirq.CCX(qubits[4], qubits[6], qubits[8])) c.append(cirq.CCX(qubits[4], qubits[8], qubits[9])) c.append(cirq.QFT(*q_qft, without_reverse=True, inverse=True)) c.append(cirq.measure(qubits[0], key='x0')) c.append(cirq.measure(qubits[1], key='x1')) c.append(cirq.measure(qubits[2], key='x2')) c.append(cirq.measure(qubits[3], key='x3')) c.append(cirq.measure(qubits[4], key='x4')) result = cirq.Simulator().run(c, repetitions=100) print(c) cirq.plot_state_histogram(result)