示例#1
0
def test_free_param_equality():
    param1 = FreeParameter("theta")
    param2 = FreeParameter("phi")
    rx1 = Gate.Rx(param1)
    rx2 = Gate.Rx(param1)
    other_gate = Gate.Rx(param2)

    assert rx1 == rx2
    assert rx1 is not rx2
    assert rx1 != other_gate
    assert rx1 != param1
示例#2
0
def many_layers(n_qubits: int, n_layers: int) -> Circuit:
    """
    Function to return circuit with many layers.

    :param int n_qubits: number of qubits
    :param int n_layers: number of layers
    :return: Constructed easy circuit
    :rtype: Circuit
    """
    qubits = range(n_qubits)
    circuit = Circuit()  # instantiate circuit object
    for q in range(n_qubits):
        circuit.h(q)
    for layer in range(n_layers):
        if (layer + 1) % 100 != 0:
            for qubit in range(len(qubits)):
                angle = np.random.uniform(0, 2 * math.pi)
                gate = np.random.choice(
                    [Gate.Rx(angle), Gate.Ry(angle), Gate.Rz(angle), Gate.H()], 1, replace=True
                )[0]
                circuit.add_instruction(Instruction(gate, qubit))
        else:
            for q in range(0, n_qubits, 2):
                circuit.cnot(q, q + 1)
            for q in range(1, n_qubits - 1, 2):
                circuit.cnot(q, q + 1)
    return circuit
示例#3
0
def test_equality():
    gate_1 = Gate(qubit_count=1, ascii_symbols=["foo"])
    gate_2 = Gate(qubit_count=1, ascii_symbols=["bar"])
    other_gate = Gate.Rx(angle=0.34)
    non_gate = "non gate"

    assert gate_1 == gate_2
    assert gate_1 is not gate_2
    assert gate_1 != other_gate
    assert gate_1 != non_gate
示例#4
0
@pytest.mark.parametrize(
    "testclass,subroutine_name,irclass,irsubclasses,kwargs", testdata)
def test_ir_gate_level(testclass, subroutine_name, irclass, irsubclasses,
                       kwargs):
    expected = irclass(**create_valid_ir_input(irsubclasses))
    actual = testclass(
        **create_valid_gate_class_input(irsubclasses, **kwargs)).to_ir(
            **create_valid_target_input(irsubclasses))
    assert actual == expected


@pytest.mark.parametrize(
    "gate, target, serialization_properties, expected_ir",
    [
        (
            Gate.Rx(angle=0.17),
            [4],
            OpenQASMSerializationProperties(
                qubit_reference_type=QubitReferenceType.VIRTUAL),
            "rx(0.17) q[4];",
        ),
        (
            Gate.Rx(angle=0.17),
            [4],
            OpenQASMSerializationProperties(
                qubit_reference_type=QubitReferenceType.PHYSICAL),
            "rx(0.17) $4;",
        ),
        (
            Gate.X(),
            [4],
示例#5
0
def test_str_angle():
    gate = Gate.Rx(0.5)
    expected = "{}('angle': {}, 'qubit_count': {})".format(
        gate.name, gate.angle, gate.qubit_count)
    assert str(gate) == expected
示例#6
0
def test_str_angle():
    gate = Gate.Rx(0.5)
    expected = f"{gate.name}('angle': {gate.angle}, 'qubit_count': {gate.qubit_count})"
    assert str(gate) == expected