示例#1
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
示例#2
0
def test_FallbackQCircuitSymbolsGate():
    class TestGate(cirq.Gate):
        def __str__(self):
            return 'T'

    g = TestGate()
    f = _FallbackQCircuitGate(g)
    assert f.qcircuit_diagram_info(
        cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT) == ('\\text{T:0}', )
    assert f.qcircuit_diagram_info(
        cirq.TextDiagramInfoArgs(known_qubits=None,
                                 known_qubit_count=2,
                                 use_unicode_characters=True,
                                 precision=None)) == ('\\text{T:0}',
                                                      '\\text{T:1}')
示例#3
0
def test_measurement_gate_diagram():
    # Shows key.
    assert cirq.MeasurementGate().text_diagram_info(
        cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT) == cirq.TextDiagramInfo(
            ("M('')",))
    assert cirq.MeasurementGate(key='test').text_diagram_info(
        cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT) == cirq.TextDiagramInfo(
            ("M('test')",))

    # Uses known qubit count.
    assert cirq.MeasurementGate().text_diagram_info(
        cirq.TextDiagramInfoArgs(
            known_qubits=None,
            known_qubit_count=3,
            use_unicode_characters=True,
            precision=None,
            qubit_map=None
        )) == cirq.TextDiagramInfo(("M('')", 'M', 'M'))

    # Shows invert mask.
    assert cirq.MeasurementGate(invert_mask=(False, True)).text_diagram_info(
        cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT) == cirq.TextDiagramInfo(
            ("M('')", "!M"))

    # Omits key when it is the default.
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')
    cirq.testing.assert_has_diagram(
        cirq.Circuit.from_ops(cirq.measure(a, b)), """
a: ───M───
      │
b: ───M───
""")
    cirq.testing.assert_has_diagram(
        cirq.Circuit.from_ops(cirq.measure(a, b, invert_mask=(True,))), """
a: ───!M───
      │
b: ───M────
""")
    cirq.testing.assert_has_diagram(
        cirq.Circuit.from_ops(cirq.measure(a, b, key='test')), """
a: ───M('test')───
      │
b: ───M───────────
""")