Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_grover_circuit(input_qubits, output_qubit, oracle):
"""Find the value recognized by the oracle in sqrt(N) attempts."""
# For 2 input qubits, that means using Grover operator only once.
c = cirq.Circuit()
# Initialize qubits.
c.append([
cirq.X(output_qubit),
cirq.H(output_qubit),
cirq.H.on_each(*input_qubits),
])
# Query oracle.
c.append(oracle)
# Construct Grover operator.
c.append(cirq.H.on_each(*input_qubits))
c.append(cirq.X.on_each(*input_qubits))
c.append(cirq.H.on(input_qubits[1]))
c.append(cirq.CNOT(input_qubits[0], input_qubits[1]))
c.append(cirq.H.on(input_qubits[1]))
c.append(cirq.X.on_each(*input_qubits))
c.append(cirq.H.on_each(*input_qubits))
# Measure the result.
def your_circuit(oracle):
"""Yields a circuit for the Deutsch-Jozsa algorithm on three qubits."""
# phase kickback trick
yield cirq.X(q2), cirq.H(q2)
# equal superposition over input bits
yield cirq.H(q0), cirq.H(q1)
# query the function
yield oracle
# interference to get result, put last qubit into |1>
yield cirq.H(q0), cirq.H(q1), cirq.H(q2)
# a final OR gate to put result in final qubit
yield cirq.X(q0), cirq.X(q1), cirq.CCX(q0, q1, q2)
yield cirq.measure(q2)
def make_bernstein_vazirani_circuit(input_qubits, output_qubit, oracle):
"""Solves for factors in f(a) = a·factors + bias (mod 2) with one query."""
c = cirq.Circuit()
# Initialize qubits.
c.append([
cirq.X(output_qubit),
cirq.H(output_qubit),
cirq.H.on_each(*input_qubits),
])
# Query oracle.
c.append(oracle)
# Measure in X basis.
c.append([
cirq.H.on_each(*input_qubits),
cirq.measure(*input_qubits, key='result')
])
return c
def generate_2x2_grid_qft_circuit():
# Define a 2*2 square grid of qubits.
a,b,c,d = [cirq.GridQubit(0, 0), cirq.GridQubit(0, 1),
cirq.GridQubit(1, 1), cirq.GridQubit(1, 0)]
circuit = cirq.Circuit.from_ops(
cirq.H(a),
_cz_and_swap(a, b, 0.5),
_cz_and_swap(b, c, 0.25),
_cz_and_swap(c, d, 0.125),
cirq.H(a),
_cz_and_swap(a, b, 0.5),
_cz_and_swap(b, c, 0.25),
cirq.H(a),
_cz_and_swap(a, b, 0.5),
cirq.H(a),
strategy=cirq.InsertStrategy.EARLIEST
)
return circuit
def make_grover_circuit(input_qubits, output_qubit, oracle):
"""Find the value recognized by the oracle in sqrt(N) attempts."""
# For 2 input qubits, that means using Grover operator only once.
c = cirq.Circuit()
# Initialize qubits.
c.append([
cirq.X(output_qubit),
cirq.H(output_qubit),
cirq.H.on_each(*input_qubits),
])
# Query oracle.
c.append(oracle)
# Construct Grover operator.
c.append(cirq.H.on_each(*input_qubits))
c.append(cirq.X.on_each(*input_qubits))
c.append(cirq.H.on(input_qubits[1]))
c.append(cirq.CNOT(input_qubits[0], input_qubits[1]))
c.append(cirq.H.on(input_qubits[1]))
c.append(cirq.X.on_each(*input_qubits))
c.append(cirq.H.on_each(*input_qubits))
# Measure the result.
c.append(cirq.measure(*input_qubits, key='result'))
cirq.CNOT(a, b),
controlled_rotations[1](b, c),
cirq.CNOT(b, a),
cirq.CNOT(a, b),
controlled_rotations[2](b, c),
cirq.CNOT(a, b),
controlled_rotations[3](b, c)
]))
controlled_swaps = [
[cirq.CNOT(c, d), cirq.H(c)],
cirq.CNOT(d, c),
controlled_rotations,
cirq.CNOT(d, c),
[cirq.inverse(op) for op in reversed(controlled_rotations)],
[cirq.H(c), cirq.CNOT(c, d)],
]
return [basis_change, controlled_swaps, basis_change[::-1]]
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 _measure_bit_of_phase(ancilla_qubit: cirq.QubitId,
system_qubits: Sequence[cirq.QubitId],
controlled_unitary: cirq.OP_TREE,
feedback_half_turns: float) -> cirq.OP_TREE:
yield cirq.H(ancilla_qubit)
yield controlled_unitary
yield cirq.Z(ancilla_qubit)**feedback_half_turns
yield cirq.H(ancilla_qubit)
yield cirq.MeasurementGate('ancilla_qubit').on(ancilla_qubit)