コード例 #1
0
def test_w_potential_implementation():
    assert not cirq.can_cast(cirq.KnownMatrix,
                             cg.ExpWGate(half_turns=cirq.Symbol('a')))
    assert not cirq.can_cast(cirq.ReversibleEffect,
                             cg.ExpWGate(half_turns=cirq.Symbol('a')))
    assert cirq.can_cast(cirq.KnownMatrix, cg.ExpWGate())
    assert cirq.can_cast(cirq.ReversibleEffect, cg.ExpWGate())
コード例 #2
0
def test_bounded_effect():
    q = cirq.NamedQubit('q')

    # If the gate isn't bounded, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.BoundedEffect, op0)
    with pytest.raises(TypeError):
        _ = op0.trace_distance_bound()

    op1 = cirq.GateOperation(cirq.Z**0.000001, [q])
    assert cirq.can_cast(cirq.BoundedEffect, op1)
    assert op1.trace_distance_bound() == (
        cirq.Z**0.000001).trace_distance_bound()
コード例 #3
0
def test_bounded_effect():
    q = cirq.NamedQubit('q')

    # If the gate isn't bounded, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.BoundedEffect, op0)
    with pytest.raises(TypeError):
        _ = op0.trace_distance_bound()

    op1 = cirq.GateOperation(cirq.Z**0.000001, [q])
    assert cirq.can_cast(cirq.BoundedEffect, op1)
    assert op1.trace_distance_bound() == (cirq.Z**0.000001
                                          ).trace_distance_bound()
コード例 #4
0
def test_inverse():
    q = cirq.NamedQubit('q')

    # If the gate isn't reversible, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.ReversibleEffect, op0)
    with pytest.raises(TypeError):
        _ = op0.inverse()

    op1 = cirq.GateOperation(cirq.S, [q])
    assert cirq.can_cast(cirq.ReversibleEffect, op1)
    assert op1.inverse() == cirq.GateOperation(cirq.S.inverse(), [q])
    assert cirq.S.inverse().on(q) == cirq.S.on(q).inverse()
コード例 #5
0
ファイル: gate_operation_test.py プロジェクト: yongwww/Cirq
def test_inverse():
    q = cirq.NamedQubit('q')

    # If the gate isn't reversible, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.ReversibleEffect, op0)
    with pytest.raises(TypeError):
        _ = op0.inverse()

    op1 = cirq.GateOperation(cirq.S, [q])
    assert cirq.can_cast(cirq.ReversibleEffect, op1)
    assert op1.inverse() == cirq.GateOperation(cirq.S.inverse(), [q])
    assert cirq.S.inverse().on(q) == cirq.S.on(q).inverse()
コード例 #6
0
def test_text_diagrammable():
    q = cirq.NamedQubit('q')

    # If the gate isn't diagrammable, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.TextDiagrammable, op0)
    with pytest.raises(TypeError):
        _ = op0.text_diagram_info(cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT)

    op1 = cirq.GateOperation(cirq.S, [q])
    assert cirq.can_cast(cirq.TextDiagrammable, op1)
    actual = op1.text_diagram_info(cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT)
    expected = cirq.S.text_diagram_info(
        cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT)
    assert actual == expected
コード例 #7
0
ファイル: gate_operation_test.py プロジェクト: yongwww/Cirq
def test_text_diagrammable():
    q = cirq.NamedQubit('q')

    # If the gate isn't diagrammable, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.TextDiagrammable, op0)
    with pytest.raises(TypeError):
        _ = op0.text_diagram_info(cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT)

    op1 = cirq.GateOperation(cirq.S, [q])
    assert cirq.can_cast(cirq.TextDiagrammable, op1)
    actual = op1.text_diagram_info(cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT)
    expected = cirq.S.text_diagram_info(
        cirq.TextDiagramInfoArgs.UNINFORMED_DEFAULT)
    assert actual == expected
コード例 #8
0
def test_extrapolate():
    q = cirq.NamedQubit('q')

    # If the gate isn't extrapolatable, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.ExtrapolatableEffect, op0)
    with pytest.raises(TypeError):
        _ = op0.extrapolate_effect(0.5)
    with pytest.raises(TypeError):
        _ = op0**0.5

    op1 = cirq.GateOperation(cirq.Y, [q])
    assert cirq.can_cast(cirq.ExtrapolatableEffect, op1)
    assert op1**0.5 == op1.extrapolate_effect(0.5) == cirq.GateOperation(
        cirq.Y**0.5, [q])
    assert (cirq.Y**0.5).on(q) == cirq.Y(q)**0.5
コード例 #9
0
def test_extrapolate():
    q = cirq.NamedQubit('q')

    # If the gate isn't extrapolatable, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.ExtrapolatableEffect, op0)
    with pytest.raises(TypeError):
        _ = op0.extrapolate_effect(0.5)
    with pytest.raises(TypeError):
        _ = op0**0.5

    op1 = cirq.GateOperation(cirq.Y, [q])
    assert cirq.can_cast(cirq.ExtrapolatableEffect, op1)
    assert op1**0.5 == op1.extrapolate_effect(0.5) == cirq.GateOperation(
        cirq.Y**0.5, [q])
    assert (cirq.Y**0.5).on(q) == cirq.Y(q)**0.5
コード例 #10
0
def test_parameterizable_effect():
    q = cirq.NamedQubit('q')
    r = cirq.ParamResolver({'a': 0.5})

    # If the gate isn't parameterizable, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.ParameterizableEffect, op0)
    with pytest.raises(TypeError):
        _ = op0.is_parameterized()
    with pytest.raises(TypeError):
        _ = op0.with_parameters_resolved_by(r)

    op1 = cirq.GateOperation(cirq.RotZGate(half_turns=cirq.Symbol('a')), [q])
    assert cirq.can_cast(cirq.ParameterizableEffect, op1)
    assert op1.is_parameterized()
    op2 = op1.with_parameters_resolved_by(r)
    assert not op2.is_parameterized()
    assert op2 == cirq.S.on(q)
コード例 #11
0
ファイル: gate_operation_test.py プロジェクト: yongwww/Cirq
def test_parameterizable_effect():
    q = cirq.NamedQubit('q')
    r = cirq.ParamResolver({'a': 0.5})

    # If the gate isn't parameterizable, you get a type error.
    op0 = cirq.GateOperation(cirq.Gate(), [q])
    assert not cirq.can_cast(cirq.ParameterizableEffect, op0)
    with pytest.raises(TypeError):
        _ = op0.is_parameterized()
    with pytest.raises(TypeError):
        _ = op0.with_parameters_resolved_by(r)

    op1 = cirq.GateOperation(cirq.RotZGate(half_turns=cirq.Symbol('a')), [q])
    assert cirq.can_cast(cirq.ParameterizableEffect, op1)
    assert op1.is_parameterized()
    op2 = op1.with_parameters_resolved_by(r)
    assert not op2.is_parameterized()
    assert op2 == cirq.S.on(q)
コード例 #12
0
def test_known_matrix():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')

    # If the gate has no matrix, you get a type error.
    op0 = cirq.measure(a)
    assert not cirq.can_cast(cirq.KnownMatrix, op0)
    with pytest.raises(TypeError):
        _ = op0.matrix()

    op1 = cirq.X(a)
    assert cirq.can_cast(cirq.KnownMatrix, op1)
    np.testing.assert_allclose(op1.matrix(),
                               np.array([[0, 1], [1, 0]]))
    op2 = cirq.CNOT(a, b)
    op3 = cirq.CNOT(a, b)
    np.testing.assert_allclose(op2.matrix(), cirq.CNOT.matrix())
    np.testing.assert_allclose(op3.matrix(), cirq.CNOT.matrix())
コード例 #13
0
def test_known_matrix():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')

    # If the gate has no matrix, you get a type error.
    op0 = cirq.measure(a)
    assert not cirq.can_cast(cirq.KnownMatrix, op0)
    with pytest.raises(TypeError):
        _ = op0.matrix()

    op1 = cirq.X(a)
    assert cirq.can_cast(cirq.KnownMatrix, op1)
    np.testing.assert_allclose(op1.matrix(),
                               np.array([[0, 1], [1, 0]]),
                               atol=1e-8)
    op2 = cirq.CNOT(a, b)
    op3 = cirq.CNOT(a, b)
    np.testing.assert_allclose(op2.matrix(), cirq.CNOT.matrix(), atol=1e-8)
    np.testing.assert_allclose(op3.matrix(), cirq.CNOT.matrix(), atol=1e-8)
コード例 #14
0
ファイル: cast_test.py プロジェクト: sheshuguang/qutrits
def test_cast():
    desired = DesiredType()
    child = ChildType()
    unrelated = UnrelatedType()
    potential = PotentialType()

    assert cirq.try_cast(DesiredType, desired) is desired
    assert cirq.try_cast(DesiredType, child) is child
    assert cirq.try_cast(DesiredType, potential) is not None
    assert cirq.try_cast(DesiredType, unrelated) is None
    assert cirq.try_cast(DesiredType, object()) is None
    assert cirq.try_cast(UnrelatedType, potential) is None

    assert cirq.can_cast(DesiredType, desired)
    assert cirq.can_cast(DesiredType, child)
    assert cirq.can_cast(DesiredType, potential)
    assert not cirq.can_cast(DesiredType, unrelated)
    assert not cirq.can_cast(DesiredType, object())
    assert not cirq.can_cast(UnrelatedType, potential)

    assert cirq.cast(DesiredType, desired) is desired
    assert cirq.cast(DesiredType, child) is child
    assert cirq.cast(DesiredType, potential) is not None
    with pytest.raises(TypeError):
        _ = cirq.cast(DesiredType, unrelated)
    with pytest.raises(TypeError):
        _ = cirq.cast(DesiredType, object())
    with pytest.raises(TypeError):
        _ = cirq.cast(UnrelatedType, potential)
コード例 #15
0
ファイル: xmon_gates_test.py プロジェクト: zhoudaqing/Cirq
def test_cz_potential_implementation():
    assert not cirq.can_cast(cirq.KnownMatrix,
                             cg.Exp11Gate(half_turns=Symbol('a')))
    assert cirq.can_cast(cirq.KnownMatrix, cg.Exp11Gate())