Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cirq_to_circuit():
q0 = cq.LineQubit(0)
q1 = cq.LineQubit(1)
q2 = cq.LineQubit(2)
gate = cirq_to_circuit(cq.Circuit(cq.X(q0)))[0]
assert isinstance(gate, qf.X)
assert gate.qubits == (0,)
gate = cirq_to_circuit(cq.Circuit(cq.X(q1)**0.4))[0]
assert isinstance(gate, qf.TX)
assert gate.qubits == (1,)
gate = cirq_to_circuit(cq.Circuit(cq.CZ(q1, q0)))[0]
assert isinstance(gate, qf.CZ)
assert gate.qubits == (1, 0)
gate = cirq_to_circuit(cq.Circuit(cq.CZ(q1, q0) ** 0.3))[0]
assert isinstance(gate, qf.CZPow)
assert gate.qubits == (1, 0)
assert gate.params['t'] == 0.3
(qml.PauliX(wires=[0]).inv(), [cirq.X ** -1]),
(qml.PauliY(wires=[0]).inv(), [cirq.Y ** -1]),
(qml.PauliZ(wires=[0]).inv(), [cirq.Z ** -1]),
(qml.Hadamard(wires=[0]), [cirq.H]),
(qml.Hadamard(wires=[0]).inv(), [cirq.H ** -1]),
(qml.S(wires=[0]), [cirq.S]),
(qml.S(wires=[0]).inv(), [cirq.S ** -1]),
(qml.PhaseShift(1.4, wires=[0]), [cirq.ZPowGate(exponent=1.4 / np.pi)]),
(qml.PhaseShift(-1.2, wires=[0]), [cirq.ZPowGate(exponent=-1.2 / np.pi)]),
(qml.PhaseShift(2, wires=[0]), [cirq.ZPowGate(exponent=2 / np.pi)]),
(
qml.PhaseShift(1.4, wires=[0]).inv(),
[cirq.ZPowGate(exponent=-1.4 / np.pi)],
),
(
qml.PhaseShift(-1.2, wires=[0]).inv(),
[cirq.ZPowGate(exponent=1.2 / np.pi)],
"""
compiled_circuit = circuit.copy()
# Optionally add some the parity check bits.
parity_map: Dict[cirq.Qid, cirq.Qid] = {} # original -> parity
if add_readout_error_correction:
num_qubits = len(compiled_circuit.all_qubits())
# Sort just to make it deterministic.
for idx, qubit in enumerate(sorted(compiled_circuit.all_qubits())):
# For each qubit, create a new qubit that will serve as its parity
# check. An inverse-controlled-NOT is performed on the qubit and its
# parity bit. Later, these two qubits will be checked for parity -
# if they are equal, there was likely a readout error.
qubit_num = idx + num_qubits
parity_qubit = cirq.LineQubit(qubit_num)
compiled_circuit.append(cirq.X(qubit))
compiled_circuit.append(cirq.CNOT(qubit, parity_qubit))
compiled_circuit.append(cirq.X(qubit))
parity_map[qubit] = parity_qubit
# Swap Mapping (Routing). Ensure the gates can actually operate on the
# target qubits given our topology.
if router is None and routing_algo_name is None:
# TODO: The routing algorithm sometimes does a poor job with the parity
# qubits, adding SWAP gates that are unnecessary. This should be fixed,
# or we can add the parity qubits manually after routing.
routing_algo_name = 'greedy'
swap_networks: List[ccr.SwapNetwork] = []
for _ in range(routing_attempts):
swap_network = ccr.route_circuit(compiled_circuit,
ccr.xmon_device_to_graph(device),
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)
phi = np.pi / 2
xi = np.pi / 2
else:
phi = np.arcsin(np.sqrt(2) * np.sin(theta_prime / 4))
xi = np.arctan(np.tan(phi) / np.sqrt(2))
yield cirq.Rz(sign * 0.5 * theta_prime).on(a)
yield cirq.Rz(sign * 0.5 * theta_prime).on(b)
yield cirq.Rx(xi).on(a)
yield cirq.X(b)**(-sign * 0.5)
yield SQRT_ISWAP_INV(a, b)
yield cirq.Rx(-2 * phi).on(a)
yield SQRT_ISWAP(a, b)
yield cirq.Rx(xi).on(a)
yield cirq.X(b)**(sign * 0.5)
# Corrects global phase
yield cirq.GlobalPhaseOperation(np.exp(sign * theta_prime * 0.25j))
the Hamiltonian XY+YX to XX+YY type. The time evolution of the XX + YY
Hamiltonian can be expressed as a power of the iSWAP gate.
Args:
p: the first qubit
q: the second qubit
theta: The rotational angle that specifies the Bogoliubov
transformation, which is a function of the kinetic energy and
the superconducting gap.
"""
# The iSWAP gate corresponds to evolve under the Hamiltonian XX+YY for
# time -pi/4.
expo = -4 * theta / np.pi
yield cirq.X(p)
yield cirq.S(p)
yield cirq.ISWAP(p, q)**expo
yield cirq.S(p) ** 1.5
yield cirq.X(p)
# For sign = 1: theta. For sign = -1, 2pi-theta
theta_prime = (sympy.pi - sign * sympy.pi) + sign * theta
phi = sympy.asin(np.sqrt(2) * sympy.sin(theta_prime / 4))
xi = sympy.atan(sympy.tan(phi) / np.sqrt(2))
yield cirq.Rz(sign * 0.5 * theta_prime).on(a)
yield cirq.Rz(sign * 0.5 * theta_prime).on(b)
yield cirq.Rx(xi).on(a)
yield cirq.X(b)**(-sign * 0.5)
yield SQRT_ISWAP_INV(a, b)
yield cirq.Rx(-2 * phi).on(a)
yield SQRT_ISWAP(a, b)
yield cirq.Rx(xi).on(a)
yield cirq.X(b)**(sign * 0.5)
def mixer(beta):
"""Generator for U(H_B, beta) layer (mixing layer)"""
for row in qreg:
for qubit in row:
yield cirq.X(qubit)**beta
p: the first qubit
q: the second qubit
theta: The rotational angle that specifies the Bogoliubov
transformation, which is a function of the kinetic energy and
the superconducting gap.
"""
# The iSWAP gate corresponds to evolve under the Hamiltonian XX+YY for
# time -pi/4.
expo = -4 * theta / np.pi
yield cirq.X(p)
yield cirq.S(p)
yield cirq.ISWAP(p, q)**expo
yield cirq.S(p) ** 1.5
yield cirq.X(p)
def _ops_from_givens_rotations_circuit_description(
qubits: Sequence[cirq.Qid],
circuit_description: Iterable[Iterable[
Union[str, Tuple[int, int, float, float]]]]) -> cirq.OP_TREE:
"""Yield operations from a Givens rotations circuit obtained from
OpenFermion.
"""
for parallel_ops in circuit_description:
for op in parallel_ops:
if op == 'pht':
yield cirq.X(qubits[-1])
else:
i, j, theta, phi = cast(Tuple[int, int, float, float], op)
yield Ryxxy(theta).on(qubits[i], qubits[j])
yield cirq.Z(qubits[j]) ** (phi / numpy.pi)