예제 #1
0
def test_text_to_qcircuit_diagrammable():
    qubits = cirq.NamedQubit('x'), cirq.NamedQubit('y')

    g = cirq.SwapPowGate(exponent=0.5)
    f = _TextToQCircuitDiagrammable(g)
    qubit_map = {q: i for i, q in enumerate(qubits)}
    actual_info = f.qcircuit_diagram_info(
        cirq.CircuitDiagramInfoArgs(known_qubits=qubits,
                                    known_qubit_count=None,
                                    use_unicode_characters=True,
                                    precision=3,
                                    qubit_map=qubit_map))
    name = '{\\text{SWAP}^{0.5}}'
    expected_info = cirq.CircuitDiagramInfo(
        ('\multigate{1}' + name, '\ghost' + name),
        exponent=0.5,
        connected=False)
    assert actual_info == expected_info

    g = cirq.SWAP
    f = _TextToQCircuitDiagrammable(g)
    qubit_map = {q: i for q, i in zip(qubits, (4, 3))}
    actual_info = f.qcircuit_diagram_info(
        cirq.CircuitDiagramInfoArgs(known_qubits=qubits,
                                    known_qubit_count=None,
                                    use_unicode_characters=True,
                                    precision=3,
                                    qubit_map=qubit_map))
    expected_info = cirq.CircuitDiagramInfo(
        ('\ghost{\\text{SWAP}}', '\multigate{1}{\\text{SWAP}}'),
        connected=False)
    assert actual_info == expected_info

    qubit_map = {q: i for q, i in zip(qubits, (2, 5))}
    actual_info = f.qcircuit_diagram_info(
        cirq.CircuitDiagramInfoArgs(known_qubits=qubits,
                                    known_qubit_count=None,
                                    use_unicode_characters=True,
                                    precision=3,
                                    qubit_map=qubit_map))
    expected_info = cirq.CircuitDiagramInfo(('\\gate{\\text{×}}', ) * 2)
    assert actual_info == expected_info

    actual_info = f.qcircuit_diagram_info(
        cirq.CircuitDiagramInfoArgs.UNINFORMED_DEFAULT)
    assert actual_info == expected_info
예제 #2
0
def test_TextToQCircuitDiagrammable():
    qubits = cirq.NamedQubit('x'), cirq.NamedQubit('y')

    g = cirq.SwapGate(half_turns=0.5)
    f = _TextToQCircuitDiagrammable(g)
    qubit_map = {q: i for i, q in enumerate(qubits)}
    actual_info = f.qcircuit_diagram_info(
            cirq.TextDiagramInfoArgs(known_qubits=qubits,
                                     known_qubit_count=None,
                                     use_unicode_characters=True,
                                     precision=3,
                                     qubit_map=qubit_map))
    name = '{\\text{SWAP}^{0.5}}'
    expected_info = cirq.TextDiagramInfo(
            ('\multigate{1}' + name, '\ghost' + name),
            exponent=0.5,
            connected=False)
    assert actual_info == expected_info

    g = cirq.SWAP
    f = _TextToQCircuitDiagrammable(g)
    qubit_map = {q: i for q, i in zip(qubits, (4, 3))}
    actual_info = f.qcircuit_diagram_info(
            cirq.TextDiagramInfoArgs(known_qubits=qubits,
                                     known_qubit_count=None,
                                     use_unicode_characters=True,
                                     precision=3,
                                     qubit_map=qubit_map))
    expected_info = cirq.TextDiagramInfo(
            ('\ghost{\\text{SWAP}}', '\multigate{1}{\\text{SWAP}}'),
            connected=False)
    assert actual_info == expected_info

    qubit_map = {q: i for q, i in zip(qubits, (2, 5))}
    actual_info = f.qcircuit_diagram_info(
            cirq.TextDiagramInfoArgs(known_qubits=qubits,
                                     known_qubit_count=None,
                                     use_unicode_characters=True,
                                     precision=3,
                                     qubit_map=qubit_map))
    expected_info = cirq.TextDiagramInfo(('\\gate{\\text{×}}',) * 2)
    assert actual_info == expected_info

    actual_info = f.qcircuit_diagram_info(
            cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT)
    assert actual_info == expected_info
예제 #3
0
def _wrap_operation(op: ops.Operation) -> ops.Operation:
    new_qubits = [_QCircuitQubit(e) for e in op.qubits]
    diagrammable = fallback_qcircuit_extensions.try_cast(  # type: ignore
        QCircuitDiagrammable, op)
    if diagrammable is None:
        info = protocols.circuit_diagram_info(op, default=None)
        if info is not None:
            diagrammable = _TextToQCircuitDiagrammable(
                cast(protocols.SupportsCircuitDiagramInfo, op))
        else:
            diagrammable = _FallbackQCircuitGate(
                cast(ops.GateOperation, op).gate)
    return _QCircuitOperation(op, diagrammable).with_qubits(*new_qubits)
예제 #4
0
def _wrap_operation(op: ops.Operation) -> ops.Operation:
    new_qubits = [_QCircuitQubit(e) for e in op.qubits]
    diagrammable = known_qcircuit_operation_symbols(op)
    if diagrammable is None:
        info = protocols.circuit_diagram_info(op, default=None)
        if info is not None:
            diagrammable = _TextToQCircuitDiagrammable(
                cast(protocols.SupportsCircuitDiagramInfo, op))
        elif isinstance(op, ops.GateOperation):
            diagrammable = _FallbackQCircuitGate(op.gate)
        else:
            diagrammable = _FallbackQCircuitGate(op)
    return _QCircuitOperation(op, diagrammable).with_qubits(*new_qubits)