def _translate_cirq_operation_to_braket_instruction( op: cirq_ops.Operation, ) -> List[Instruction]: """Converts the Cirq operation to an equivalent Braket instruction or list of instructions. Args: op: Cirq operation to convert. Raises: ValueError: If the operation cannot be converted to Braket. """ nqubits = protocols.num_qubits(op) if nqubits == 1: return _translate_one_qubit_cirq_operation_to_braket_instruction(op) elif nqubits == 2: return _translate_two_qubit_cirq_operation_to_braket_instruction(op) elif nqubits == 3: qubits = [q.x for q in op.qubits] if isinstance(op.gate, cirq_ops.TOFFOLI): return [Instruction(braket_gates.CCNot(), qubits)] elif isinstance(op.gate, cirq_ops.FREDKIN): return [Instruction(braket_gates.CSwap(), qubits)] # Unsupported gates. else: raise ValueError( f"Unable to convert {op} to Braket. If you think this is a bug, " "you can open an issue on the Mitiq GitHub at" " https://github.com/unitaryfund/mitiq.")
def _translate_cirq_operation_to_braket_instruction( op: cirq_ops.Operation, ) -> List[Instruction]: """Converts the Cirq operation to an equivalent Braket instruction or list of instructions. Args: op: Cirq operation to convert. Raises: ValueError: If the operation cannot be converted to Braket. """ nqubits = protocols.num_qubits(op) if nqubits == 1: return _translate_one_qubit_cirq_operation_to_braket_instruction(op) elif nqubits == 2: return _translate_two_qubit_cirq_operation_to_braket_instruction(op) elif nqubits == 3: qubits = [q.x for q in op.qubits] if op == cirq_ops.TOFFOLI.on(*op.qubits): return [Instruction(braket_gates.CCNot(), qubits)] elif op == cirq_ops.FREDKIN.on(*op.qubits): return [Instruction(braket_gates.CSwap(), qubits)] else: _raise_cirq_to_braket_error(op) # Unsupported gates. else: _raise_cirq_to_braket_error(op)
def test_from_braket_three_qubit_gates(): braket_circuit = BKCircuit() instructions = [ Instruction(braket_gates.CCNot(), target=[1, 2, 3]), Instruction(braket_gates.CSwap(), target=[1, 2, 3]), ] for instr in instructions: braket_circuit.add_instruction(instr) cirq_circuit = from_braket(braket_circuit) qreg = LineQubit.range(1, 4) expected_cirq_circuit = Circuit(ops.TOFFOLI(*qreg), ops.FREDKIN(*qreg)) assert np.allclose( protocols.unitary(cirq_circuit), protocols.unitary(expected_cirq_circuit), )
def _(toffoli: qml.Toffoli, _parameters): return gates.CCNot()
(Circuit().xy(1, 0, 0.15), gates.XY(0.15).to_matrix()), (Circuit().xy(0, 1, 0.15), gates.XY(0.15).to_matrix()), (Circuit().cphaseshift(1, 0, 0.15), gates.CPhaseShift(0.15).to_matrix()), (Circuit().cphaseshift00(1, 0, 0.15), gates.CPhaseShift00(0.15).to_matrix()), (Circuit().cphaseshift01(1, 0, 0.15), gates.CPhaseShift01(0.15).to_matrix()), (Circuit().cphaseshift10(1, 0, 0.15), gates.CPhaseShift10(0.15).to_matrix()), (Circuit().cy(1, 0), gates.CY().to_matrix()), (Circuit().cz(1, 0), gates.CZ().to_matrix()), (Circuit().xx(1, 0, 0.15), gates.XX(0.15).to_matrix()), (Circuit().yy(1, 0, 0.15), gates.YY(0.15).to_matrix()), (Circuit().zz(1, 0, 0.15), gates.ZZ(0.15).to_matrix()), (Circuit().ccnot(2, 1, 0), gates.CCNot().to_matrix()), ( Circuit().ccnot(2, 1, 0).add_result_type( ResultType.Expectation(observable=Observable.Y(), target=[1])), gates.CCNot().to_matrix(), ), (Circuit().ccnot(1, 2, 0), gates.CCNot().to_matrix()), (Circuit().cswap(2, 1, 0), gates.CSwap().to_matrix()), (Circuit().cswap(2, 0, 1), gates.CSwap().to_matrix()), (Circuit().h(1), np.kron(gates.H().to_matrix(), np.eye(2))), (Circuit().x(1).i(2), np.kron(np.eye(2), np.kron(gates.X().to_matrix(), np.eye(2)))), ( Circuit().y(1).z(2), np.kron(gates.Z().to_matrix(), np.kron(gates.Y().to_matrix(), np.eye(2))),