Beispiel #1
0
def test_extensions():
    # We test that an extension is being applied, by created an incorrect
    # gate with an extension.

    class WrongH(CompositeGate):
        def default_decompose(self,
                              qubits: Sequence[cirq.QubitId]) -> cirq.OP_TREE:
            return X(Q1)

    extensions = Extensions(
        desired_to_actual_to_wrapper={CompositeGate: {
            H: lambda e: WrongH()
        }})

    circuit = Circuit()
    circuit.append([WrongH()(Q1)])

    simulator = xmon_simulator.XmonSimulator()
    results = simulator.simulate(circuit, extensions=extensions)
    np.testing.assert_almost_equal(results.final_state, np.array([0, -1j]))
    def __init__(self, sub: ops.Gate) -> None:
        self.sub = sub

    def qcircuit_diagram_info(self, args: ops.TextDiagramInfoArgs
                              ) -> ops.TextDiagramInfo:
        name = str(self.sub)
        qubit_count = ((len(args.known_qubits) if
                       (args.known_qubits is not None) else 1)
                       if args.known_qubit_count is None
                       else args.known_qubit_count)
        symbols = tuple(_escape_text_for_latex('{}:{}'.format(name, i))
                        for i in range(qubit_count))
        return ops.TextDiagramInfo(symbols)


fallback_qcircuit_extensions = Extensions()
fallback_qcircuit_extensions.add_cast(
    QCircuitDiagrammable,
    ops.TextDiagrammable,
    _TextToQCircuitDiagrammable)
fallback_qcircuit_extensions.add_recursive_cast(
    QCircuitDiagrammable,
    ops.GateOperation,
    lambda ext, op: ext.try_cast(QCircuitDiagrammable, op.gate))
fallback_qcircuit_extensions.add_cast(
    QCircuitDiagrammable,
    ops.RotXGate,
    lambda gate:
        _HardcodedQCircuitSymbolsGate('\\targ')
        if gate.half_turns == 1
        else None)

class _FallbackQCircuitSymbolsGate(QCircuitDiagrammableGate):
    def __init__(self, sub: ops.Gate) -> None:
        self.sub = sub

    def qcircuit_wire_symbols(self, qubit_count=None):
        name = str(self.sub)
        if qubit_count is None:
            qubit_count = 1
        return tuple(
            _escape_text_for_latex('{}:{}'.format(name, i))
            for i in range(qubit_count))


fallback_qcircuit_extensions = Extensions()
fallback_qcircuit_extensions.add_cast(QCircuitDiagrammableGate,
                                      ops.TextDiagrammableGate,
                                      _WrappedSymbolsQCircuitGate)
fallback_qcircuit_extensions.add_cast(
    QCircuitDiagrammableGate, ops.RotXGate,
    lambda gate: _HardcodedQCircuitSymbolsGate('\\targ')
    if gate.half_turns == 1 else None)
fallback_qcircuit_extensions.add_cast(
    QCircuitDiagrammableGate, ops.MeasurementGate,
    lambda gate: _HardcodedQCircuitSymbolsGate('\\meter'))
fallback_qcircuit_extensions.add_cast(
    QCircuitDiagrammableGate, cirq.google.ExpWGate,
    lambda gate: _HardcodedQCircuitSymbolsGate('\\targ')
    if gate.half_turns == 1 and gate.axis_half_turns == 0 else None)
fallback_qcircuit_extensions.add_cast(
Beispiel #4
0
    def __init__(self, sub: ops.Gate) -> None:
        self.sub = sub

    def qcircuit_diagram_info(self, args: protocols.CircuitDiagramInfoArgs
                              ) -> protocols.CircuitDiagramInfo:
        name = str(self.sub)
        qubit_count = ((len(args.known_qubits) if
                       (args.known_qubits is not None) else 1)
                       if args.known_qubit_count is None
                       else args.known_qubit_count)
        symbols = [name] + ['#{}'.format(i + 1) for i in range(1, qubit_count)]
        escaped_symbols = tuple(_escape_text_for_latex(s)  for s in symbols)
        return protocols.CircuitDiagramInfo(escaped_symbols)


fallback_qcircuit_extensions = Extensions()
fallback_qcircuit_extensions.add_recursive_cast(  # type: ignore
    QCircuitDiagrammable,
    ops.GateOperation,
    lambda ext, op: ext.try_cast(QCircuitDiagrammable, op.gate)  # type: ignore
)
fallback_qcircuit_extensions.add_cast(  # type: ignore
    QCircuitDiagrammable,
    ops.XPowGate,
    lambda gate:
        _HardcodedQCircuitSymbolsGate('\\targ')
        if gate.exponent == 1
        else None)
fallback_qcircuit_extensions.add_cast(  # type: ignore
    QCircuitDiagrammable,
    ops.MeasurementGate,