예제 #1
0
def test_validate_protoquil_reset_qubit():
    prog = Program(
        RESET(2),
    )
    with pytest.raises(ValueError):
        validate_protoquil(prog)
    assert not prog.is_protoquil()
예제 #2
0
def test_validate_protoquil_measure_last():
    prog = Program(
        MEASURE(0),
        H(0),
    )
    with pytest.raises(ValueError):
        validate_protoquil(prog)
    assert not prog.is_protoquil()
예제 #3
0
def test_validate_protoquil_reset_first():
    prog = Program(
        H(0),
        RESET(),
    )
    with pytest.raises(ValueError):
        validate_protoquil(prog)
    assert not prog.is_protoquil()
예제 #4
0
def test_validate_protoquil_multiple_measures():
    prog = Program(
        RESET(),
        H(1),
        Pragma('DELAY'),
        MEASURE(1),
        MEASURE(1)
    )
    with pytest.raises(ValueError):
        validate_protoquil(prog)
예제 #5
0
    def run_and_measure(self, program: Program,
                        trials: int) -> Dict[int, np.ndarray]:
        """
        Run the provided state preparation program and measure all qubits.

        This will measure all the qubits on this QuantumComputer, not just qubits
        that are used in the program.

        The returned data is a dictionary keyed by qubit index because qubits for a given
        QuantumComputer may be non-contiguous and non-zero-indexed. To turn this dictionary
        into a 2d numpy array of bitstrings, consider::

            bitstrings = qc.run_and_measure(...)
            bitstring_array = np.vstack(bitstrings[q] for q in qc.qubits()).T
            bitstring_array.shape  # (trials, len(qc.qubits()))

        .. note::

            In contrast to :py:class:`QVMConnection.run_and_measure`, this method simulates
            noise correctly for noisy QVMs. However, this method is slower for ``trials > 1``.
            For faster noise-free simulation, consider
            :py:class:`WavefunctionSimulator.run_and_measure`.

        :param program: The state preparation program to run and then measure.
        :param trials: The number of times to run the program.
        :return: A dictionary keyed by qubit index where the corresponding value is a 1D array of
            measured bits.
        """
        program = program.copy()
        validate_protoquil(program)
        ro = program.declare('ro', 'BIT', len(self.qubits()))
        for i, q in enumerate(self.qubits()):
            program.inst(MEASURE(q, ro[i]))
        program.wrap_in_numshots_loop(trials)
        executable = self.compile(program)
        bitstring_array = self.run(executable=executable)
        bitstring_dict = {}
        for i, q in enumerate(self.qubits()):
            bitstring_dict[q] = bitstring_array[:, i]
        return bitstring_dict
예제 #6
0
파일: test_quil.py 프로젝트: tocheng/pyquil
def test_is_protoquil():
    prog = Program(Declare("ro", "BIT"), MEASURE(1, MemoryReference("ro", 0)),
                   H(1), RESET())
    validate_protoquil(prog)
    assert prog.is_protoquil()

    prog = (Program(Declare("ro", "BIT"), H(0), Y(1), CNOT(0, 1)).measure(
        0, MemoryReference("ro", 0)).if_then(MemoryReference("ro", 0),
                                             Program(X(0)), Program()))
    with pytest.raises(ValueError):
        validate_protoquil(prog)
    assert not prog.is_protoquil()

    prog = Program(Declare("ro", "BIT"), ClassicalNot(MemoryReference("ro",
                                                                      0)))
    with pytest.raises(ValueError):
        validate_protoquil(prog)
    assert not prog.is_protoquil()

    prog = Program(DefCalibration("I", [], [Qubit(0)], []), I(0))
    with pytest.raises(ValueError):
        validate_protoquil(prog)
    assert not prog.is_protoquil()
    assert prog.is_protoquil(quilt=True)
예제 #7
0
def test_is_protoquil():
    prog = Program(Declare('ro', 'BIT'), MEASURE(1, MemoryReference("ro", 0)),
                   H(1), RESET())
    validate_protoquil(prog)
    assert prog.is_protoquil()

    prog = Program(Declare('ro', 'BIT'), H(0), Y(1), CNOT(0, 1)) \
        .measure(0, MemoryReference("ro", 0)) \
        .if_then(MemoryReference("ro", 0), Program(X(0)), Program())
    with pytest.raises(ValueError):
        validate_protoquil(prog)
    assert not prog.is_protoquil()

    prog = Program(Declare('ro', 'BIT'), ClassicalNot(MemoryReference("ro",
                                                                      0)))
    with pytest.raises(ValueError):
        validate_protoquil(prog)
    assert not prog.is_protoquil()
예제 #8
0
def test_validate_protoquil_suite():
    validate_protoquil(
        Program("""
RESET
DECLARE ro BIT[3]
RX(-pi/4) 2
RZ(4*pi) 3
I 0
CZ 2 3
MEASURE 2 ro[2]
MEASURE 3 ro[3]
"""))

    validate_protoquil(
        Program("""
RESET
DECLARE ro BIT[3]
RX(-pi/4) 2
RZ(4*pi) 3
I 0
CZ 2 3
MEASURE 2 ro[2]
MEASURE 3 ro[3]
HALT
"""))
    validate_protoquil(
        Program("""
RESET
DECLARE ro BIT[3]
RX(-pi/4) 2
RZ(4*pi) 3
I 0
MEASURE 0
CZ 2 3
MEASURE 2 ro[2]
X 3
MEASURE 3 ro[3]
HALT
"""))

    with pytest.raises(ValueError):
        validate_protoquil(
            Program("""
RESET
DECLARE ro BIT[3]
RX(-pi/4) 2
RZ(4*pi) 3
RESET
I 0
CZ 2 3
MEASURE 2 ro[2]
MEASURE 3 ro[3]
"""))

    with pytest.raises(ValueError):
        validate_protoquil(
            Program("""
RESET
DECLARE ro BIT[3]
RX(-pi/4) 2
RZ(4*pi) 3
MEASURE 2
I 0
CZ 2 3
MEASURE 2 ro[2]
MEASURE 3 ro[3]
"""))

    with pytest.raises(ValueError):
        validate_protoquil(
            Program("""
RESET
DECLARE ro BIT[3]
RX(-pi/4) 2
RZ(4*pi) 3
HALT
I 0
CZ 2 3
MEASURE 2 ro[2]
MEASURE 3 ro[3]
"""))