Esempio n. 1
0
def test_random_circuit_errors():
    with pytest.raises(ValueError):
        random_circuit(randint(1, 10), randint(1, 10), -1)
    with pytest.raises(ValueError):
        random_circuit(randint(1, 10), randint(1, 10), 1.)

    with pytest.raises(ValueError):
        random_circuit(randint(1, 10), randint(1, 10), random(), gate_domain={})

    with pytest.raises(ValueError):
        random_circuit(0, randint(1, 10), random())

    with pytest.raises(ValueError):
        random_circuit((), randint(1, 10), random())
Esempio n. 2
0
def test_random_circuit_errors():
    with pytest.raises(ValueError):
        random_circuit(randint(1, 10), randint(1, 10), -1)
    with pytest.raises(ValueError):
        random_circuit(randint(1, 10), randint(1, 10), 1.)

    with pytest.raises(ValueError):
        random_circuit(randint(1, 10),
                       randint(1, 10),
                       random(),
                       gate_domain={})

    with pytest.raises(ValueError):
        random_circuit(0, randint(1, 10), random())

    with pytest.raises(ValueError):
        random_circuit((), randint(1, 10), random())
Esempio n. 3
0
def test_random_circuit_reproducible_with_seed(seed):
    wrappers = (lambda s: s, np.random.RandomState)
    circuits = [
        random_circuit(qubits=20,
                       n_moments=20,
                       op_density=0.7,
                       random_state=wrapper(seed)) for wrapper in wrappers
        for _ in range(2)
    ]
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(*circuits)
Esempio n. 4
0
def test_random_circuit(n_qubits, n_moments, gate_domain, op_density,
                        pass_qubits):
    qubits = ([ops.QubitId()
               for _ in range(n_qubits)] if pass_qubits else n_qubits)
    circuit = random_circuit(qubits, n_moments, op_density, gate_domain)
    if pass_qubits:
        assert circuit.all_qubits().issubset(qubits)
    assert len(circuit) == n_moments
    if gate_domain is None:
        gate_domain = DEFAULT_GATE_DOMAIN
    assert set(op.gate
               for op in circuit.all_operations()).issubset(gate_domain)
Esempio n. 5
0
def test_random_circuit_reproducible_with_seed(seed):
    circuit1 = random_circuit(qubits=20,
                              n_moments=20,
                              op_density=0.7,
                              random_state=seed)
    circuit2 = random_circuit(qubits=20,
                              n_moments=20,
                              op_density=0.7,
                              random_state=seed)
    assert circuit1 == circuit2

    prng = np.random.RandomState(seed)
    circuit1 = random_circuit(qubits=20,
                              n_moments=20,
                              op_density=0.7,
                              random_state=prng)
    prng = np.random.RandomState(seed)
    circuit2 = random_circuit(qubits=20,
                              n_moments=20,
                              op_density=0.7,
                              random_state=prng)
    assert circuit1 == circuit2
Esempio n. 6
0
def test_random_circuit(n_qubits,
                        n_moments,
                        gate_domain,
                        op_density,
                        pass_qubits
                        ):
    qubits = ([cirq.QubitId() for _ in range(n_qubits)]
              if pass_qubits else n_qubits)
    circuit = random_circuit(qubits, n_moments, op_density, gate_domain)
    if pass_qubits:
        assert circuit.all_qubits().issubset(qubits)
    assert len(circuit) == n_moments
    if gate_domain is None:
        gate_domain = DEFAULT_GATE_DOMAIN
    assert set(op.gate for op in circuit.all_operations()).issubset(gate_domain)
Esempio n. 7
0
def test_random_circuit(n_qubits: Union[int, Sequence[cirq.Qid]],
                        n_moments: int, op_density: float,
                        gate_domain: Optional[Dict[cirq.Gate,
                                                   int]], pass_qubits: bool):
    qubit_set = cirq.LineQubit.range(n_qubits)
    qubit_arg = qubit_set if pass_qubits else n_qubits
    circuit = random_circuit(qubit_arg, n_moments, op_density, gate_domain)
    if qubit_arg is qubit_set:
        assert circuit.all_qubits().issubset(qubit_set)
    assert len(circuit) == n_moments
    if gate_domain is None:
        gate_domain = DEFAULT_GATE_DOMAIN
    assert set(
        cast(cirq.GateOperation, op).gate
        for op in circuit.all_operations()).issubset(gate_domain)
Esempio n. 8
0
def test_random_circuit_reproducible_between_runs():
    circuit = random_circuit(5, 8, 0.5, random_state=77)
    expected_diagram = """
                  ┌──┐
0: ────────────────S─────iSwap───────Y───X───
                         │
1: ───────────Y──────────iSwap───────Y───────

2: ─────────────────X────T───────────S───S───
                    │
3: ───────@────────S┼────H───────────────Z───
          │         │
4: ───────@─────────@────────────────────X───
                  └──┘
    """
    cirq.testing.assert_has_diagram(circuit, expected_diagram)