def test_validate_supported_quil_reset_qubit(): prog = Program( RESET(2), ) with pytest.raises(ValueError): validate_supported_quil(prog) assert not prog.is_supported_on_qpu()
def test_validate_supported_quil_measure_last(): prog = Program( MEASURE(0), H(0), ) with pytest.raises(ValueError): validate_supported_quil(prog) assert not prog.is_supported_on_qpu()
def test_validate_supported_quil_multiple_measures(): prog = Program( RESET(), H(1), Pragma('DELAY'), MEASURE(1, None), MEASURE(1, None) ) with pytest.raises(ValueError): validate_supported_quil(prog)
def run_and_measure(self, program: Program, trials: int) -> Dict[int, np.ndarray]: """ Run the provided state preparation program and measure all qubits. 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:: If the target :py:class:`QuantumComputer` is a noiseless :py:class:`QVM` then only the qubits explicitly used in the program will be measured. Otherwise all qubits will be measured. In some circumstances this can exhaust the memory available to the simulator, and this may be manifested by the QVM failing to respond or timeout. .. 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_supported_quil(program) ro = program.declare("ro", "BIT", len(self.qubits())) measure_used = isinstance(self.qam, QVM) and self.qam.noise_model is None qubits_to_measure = set( map(qubit_index, program.get_qubits()) if measure_used else self. qubits()) for i, q in enumerate(qubits_to_measure): 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(qubits_to_measure): bitstring_dict[q] = bitstring_array[:, i] for q in set(self.qubits()) - set(qubits_to_measure): bitstring_dict[q] = np.zeros(trials) return bitstring_dict
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_supported_quil(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
def test_validate_supported_quil_suite(): validate_supported_quil( 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_supported_quil( 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_supported_quil( 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] """)) with pytest.raises(ValueError): validate_supported_quil( 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_supported_quil( 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_supported_quil( 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] """))
def test_validate_supported_quil_reset_first(): prog = Program(H(0), RESET()) with pytest.raises(ValueError): validate_supported_quil(prog) assert not prog.is_supported_on_qpu()