Beispiel #1
0
 def optimization_at(
         self, circuit: cirq.Circuit, index: int,
         op: cirq.Operation) -> Optional[cirq.PointOptimizationSummary]:
     if len(op.qubits) > 3:
         raise ValueError(f'Four qubit ops not yet supported: {op}')
     new_ops = None
     if op.gate == cirq.SWAP or op.gate == cirq.CNOT:
         new_ops = cirq.google.optimized_for_sycamore(cirq.Circuit(op))
     if isinstance(op, cirq.ControlledOperation):
         qubits = op.sub_operation.qubits
         if op.gate.sub_gate == cirq.ISWAP:
             new_ops = controlled_iswap.controlled_iswap(
                 *qubits, *op.controls)
         if op.gate.sub_gate == cirq.ISWAP**-1:
             new_ops = controlled_iswap.controlled_iswap(*qubits,
                                                         *op.controls,
                                                         inverse=True)
         if op.gate.sub_gate == cirq.ISWAP**0.5:
             new_ops = controlled_iswap.controlled_sqrt_iswap(
                 *qubits, *op.controls)
         if op.gate.sub_gate == cirq.ISWAP**-0.5:
             new_ops = controlled_iswap.controlled_inv_sqrt_iswap(
                 *qubits, *op.controls)
         if op.gate.sub_gate == cirq.X:
             if len(op.qubits) == 2:
                 new_ops = cirq.google.optimized_for_sycamore(
                     cirq.Circuit(cirq.CNOT(*op.controls, *qubits)))
             if len(op.qubits) == 3:
                 new_ops = cirq.google.optimized_for_sycamore(
                     cirq.Circuit(cirq.TOFFOLI(*op.controls, *qubits)))
     if new_ops:
         return cirq.PointOptimizationSummary(clear_span=1,
                                              clear_qubits=op.qubits,
                                              new_operations=new_ops)
def test_controlled_sqrt_iswap():
    qubits = cirq.LineQubit.range(3)
    result = controlled_iswap.controlled_sqrt_iswap(qubits[0], qubits[1],
                                                    qubits[2])
    import numpy
    numpy.set_printoptions(linewidth=200, precision=3)
    print(numpy.around(cirq.unitary(result), decimals=2))
    cirq.testing.assert_allclose_up_to_global_phase(
        cirq.unitary(result),
        cirq.unitary(
            cirq.Circuit(
                (cirq.ISWAP(qubits[0],
                            qubits[1]) ** 0.5).controlled_by(qubits[2]))),
        atol=1e-8,
    )