예제 #1
0
def test_serialize_parameterized_invalid():
    q = cirq.LineQubit(0)
    circuit = cirq.Circuit(cirq.X(q)**(sympy.Symbol('x')))
    serializer = ionq.Serializer()
    with pytest.raises(ValueError, match='parameterized'):
        _ = serializer.serialize(circuit)
예제 #2
0
def test_serialize_implicit_num_qubits():
    q0 = cirq.LineQubit(2)
    circuit = cirq.Circuit(cirq.X(q0))
    serializer = ionq.Serializer()
    result = serializer.serialize(circuit)
    assert result.body['qubits'] == 3
예제 #3
0
def test_serialize_non_terminal_measurements():
    q0 = cirq.LineQubit(0)
    circuit = cirq.Circuit(cirq.measure(q0, key='d'), cirq.X(q0))
    serializer = ionq.Serializer()
    with pytest.raises(ValueError, match='end of circuit'):
        _ = serializer.serialize(circuit)
예제 #4
0
def test_serialize_not_line_qubits_invalid():
    q0 = cirq.NamedQubit('a')
    circuit = cirq.Circuit(cirq.X(q0))
    serializer = ionq.Serializer()
    with pytest.raises(ValueError, match='NamedQubit'):
        _ = serializer.serialize(circuit)
예제 #5
0
def test_single_qubit_with_two_qubits(device):
    transformer = ct.ConnectivityHeuristicCircuitTransformer(device)
    c = cirq.Circuit(cirq.X(a1), cirq.X(a2), cirq.X(a3),
                     cirq.ISWAP(a3, a4)**0.5)
    transformer.qubit_mapping(c)
    device.validate_circuit(transformer.transform(c))
예제 #6
0
def test_pauli_string_expectation_value_pure_state():
    qubits = cirq.LineQubit.range(4)
    qubit_index_map = {q: i for i, q in enumerate(qubits)}

    circuit = cirq.Circuit.from_ops(
        cirq.X(qubits[1]),
        cirq.H(qubits[2]),
        cirq.X(qubits[3]),
        cirq.H(qubits[3]),
    )
    wavefunction = circuit.apply_unitary_effect_to_state(qubit_order=qubits)
    density_matrix = np.outer(wavefunction, np.conj(wavefunction))

    z0z1 = cirq.pauli_string_expectation(
        cirq.PauliString({
            qubits[0]: cirq.Z,
            qubits[1]: cirq.Z
        }))
    z0z2 = cirq.pauli_string_expectation(
        cirq.PauliString({
            qubits[0]: cirq.Z,
            qubits[2]: cirq.Z
        }))
    z0z3 = cirq.pauli_string_expectation(
        cirq.PauliString({
            qubits[0]: cirq.Z,
            qubits[3]: cirq.Z
        }))
    z0x1 = cirq.pauli_string_expectation(
        cirq.PauliString({
            qubits[0]: cirq.Z,
            qubits[1]: cirq.X
        }))
    z1x2 = cirq.pauli_string_expectation(
        cirq.PauliString({
            qubits[1]: cirq.Z,
            qubits[2]: cirq.X
        }))
    x0z1 = cirq.pauli_string_expectation(
        cirq.PauliString({
            qubits[0]: cirq.X,
            qubits[1]: cirq.Z
        }))
    x3 = cirq.pauli_string_expectation(cirq.PauliString({qubits[3]: cirq.X}))

    np.testing.assert_allclose(
        z0z1.value_derived_from_wavefunction(wavefunction, qubit_index_map),
        -1)
    np.testing.assert_allclose(
        z0z2.value_derived_from_wavefunction(wavefunction, qubit_index_map), 0)
    np.testing.assert_allclose(
        z0z3.value_derived_from_wavefunction(wavefunction, qubit_index_map), 0)
    np.testing.assert_allclose(
        z0x1.value_derived_from_wavefunction(wavefunction, qubit_index_map), 0)
    np.testing.assert_allclose(
        z1x2.value_derived_from_wavefunction(wavefunction, qubit_index_map),
        -1)
    np.testing.assert_allclose(
        x0z1.value_derived_from_wavefunction(wavefunction, qubit_index_map), 0)
    np.testing.assert_allclose(
        x3.value_derived_from_wavefunction(wavefunction, qubit_index_map), -1)

    np.testing.assert_allclose(
        z0z1.value_derived_from_density_matrix(density_matrix,
                                               qubit_index_map), -1)
    np.testing.assert_allclose(
        z0z2.value_derived_from_density_matrix(density_matrix,
                                               qubit_index_map), 0)
    np.testing.assert_allclose(
        z0z3.value_derived_from_density_matrix(density_matrix,
                                               qubit_index_map), 0)
    np.testing.assert_allclose(
        z0x1.value_derived_from_density_matrix(density_matrix,
                                               qubit_index_map), 0)
    np.testing.assert_allclose(
        z1x2.value_derived_from_density_matrix(density_matrix,
                                               qubit_index_map), -1)
    np.testing.assert_allclose(
        x0z1.value_derived_from_density_matrix(density_matrix,
                                               qubit_index_map), 0)
    np.testing.assert_allclose(
        x3.value_derived_from_density_matrix(density_matrix, qubit_index_map),
        -1)
예제 #7
0
def test_single_qubit_ops(device):
    transformer = ct.ConnectivityHeuristicCircuitTransformer(device)
    c = cirq.Circuit(cirq.X(a1), cirq.X(a2), cirq.X(a3))
    transformer.qubit_mapping(c)
    c = transformer.transform(c)
    device.validate_circuit(c)
예제 #8
0
# Cirs Simulaton
# 2 types
# - pure state  - supported by cirq.Simulator
# - mixed state - supported by cirq.DensityMatrixSimulator

import cirq

# Pick a qubit.
qubit = cirq.GridQubit(0, 0)
print(qubit)

# Create a circuit
circuit = cirq.Circuit(
    cirq.X(qubit)**0.5,  # Square root of NOT.
    cirq.measure(qubit, key='m')  # Measurement.
)
print("Circuit:")
print(circuit)

# Simulate the circuit several times.
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=20)
print("Results:")
print(result)

# Circuit:
# (0, 0): ───X^0.5───M('m')───
# Results:
# m=10000001010010011111
#
예제 #9
0
def test_circuit_diagram_tagged_global_phase():
    # Tests global phase operation
    q = cirq.NamedQubit('a')
    global_phase = cirq.GlobalPhaseOperation(
        coefficient=-1.0).with_tags('tag0')

    # Just global phase in a circuit
    assert cirq.circuit_diagram_info(global_phase,
                                     default='default') == 'default'
    cirq.testing.assert_has_diagram(cirq.Circuit(global_phase),
                                    "\n\nglobal phase:   π['tag0']",
                                    use_unicode_characters=True)
    cirq.testing.assert_has_diagram(
        cirq.Circuit(global_phase),
        "\n\nglobal phase:   π",
        use_unicode_characters=True,
        include_tags=False,
    )

    expected = cirq.CircuitDiagramInfo(
        wire_symbols=(),
        exponent=1.0,
        connected=True,
        exponent_qubit_index=None,
        auto_exponent_parens=True,
    )

    # Operation with no qubits and returns diagram info with no wire symbols
    class NoWireSymbols(cirq.GlobalPhaseOperation):
        def _circuit_diagram_info_(
                self, args: 'cirq.CircuitDiagramInfoArgs'
        ) -> 'cirq.CircuitDiagramInfo':
            return expected

    no_wire_symbol_op = NoWireSymbols(coefficient=-1.0).with_tags('tag0')
    assert cirq.circuit_diagram_info(no_wire_symbol_op,
                                     default='default') == expected
    cirq.testing.assert_has_diagram(
        cirq.Circuit(no_wire_symbol_op),
        "\n\nglobal phase:   π['tag0']",
        use_unicode_characters=True,
    )

    # Two global phases in one moment
    tag1 = cirq.GlobalPhaseOperation(coefficient=1j).with_tags('tag1')
    tag2 = cirq.GlobalPhaseOperation(coefficient=1j).with_tags('tag2')
    c = cirq.Circuit([cirq.X(q), tag1, tag2])
    cirq.testing.assert_has_diagram(
        c,
        """\
a: ─────────────X───────────────────

global phase:   π['tag1', 'tag2']""",
        use_unicode_characters=True,
        precision=2,
    )

    # Two moments with global phase, one with another tagged gate
    c = cirq.Circuit([cirq.X(q).with_tags('x_tag'), tag1])
    c.append(cirq.Moment([cirq.X(q), tag2]))
    cirq.testing.assert_has_diagram(
        c,
        """\
a: ─────────────X['x_tag']─────X──────────────

global phase:   0.5π['tag1']   0.5π['tag2']
""",
        use_unicode_characters=True,
        include_tags=True,
    )
예제 #10
0
def test_cannot_remap_non_measurement_gate():
    a = cirq.LineQubit(0)
    op = cirq.X(a).with_tags('tag')

    assert cirq.with_measurement_key_mapping(op, {'m': 'k'}) is NotImplemented
예제 #11
0
 def _decompose_(self, qubits):
     a, b = qubits
     yield cirq.Z(a)
     yield cirq.S(b)
     yield cirq.X(a)