def __init__( self, connection: ForestConnection, noise_model: Optional[NoiseModel] = None, gate_noise: Optional[List[float]] = None, measurement_noise: Optional[List[float]] = None, random_seed: Optional[int] = None, requires_executable: bool = False, ) -> None: """ A virtual machine that classically emulates the execution of Quil programs. :param connection: A connection to the Forest web API. :param noise_model: A noise model that describes noise to apply when emulating a program's execution. :param gate_noise: A list of three numbers [Px, Py, Pz] indicating the probability of an X, Y, or Z gate getting applied to each qubit after a gate application or reset. The default value of None indicates no noise. :param measurement_noise: A list of three numbers [Px, Py, Pz] indicating the probability of an X, Y, or Z gate getting applied before a measurement. The default value of None indicates no noise. :param random_seed: A seed for the QVM's random number generators. Either None (for an automatically generated seed) or a non-negative integer. :param requires_executable: Whether this QVM will refuse to run a :py:class:`Program` and only accept the result of :py:func:`compiler.native_quil_to_executable`. Setting this to True better emulates the behavior of a QPU. """ super().__init__() if (noise_model is not None) and (gate_noise is not None or measurement_noise is not None): raise ValueError( """ You have attempted to supply the QVM with both a Kraus noise model (by supplying a `noise_model` argument), as well as either `gate_noise` or `measurement_noise`. At this time, only one may be supplied. To read more about supplying noise to the QVM, see http://pyquil.readthedocs.io/en/latest/noise_models.html#support-for-noisy-gates-on-the-rigetti-qvm. """ ) self.noise_model = noise_model self.connection = connection validate_noise_probabilities(gate_noise) validate_noise_probabilities(measurement_noise) self.gate_noise = gate_noise self.measurement_noise = measurement_noise if random_seed is None: self.random_seed = None elif isinstance(random_seed, int) and random_seed >= 0: self.random_seed = random_seed else: raise TypeError("random_seed should be None or a non-negative int") self.requires_executable = requires_executable self.connect()
def test_validate_noise_probabilities(): with pytest.raises(TypeError): validate_noise_probabilities(1) with pytest.raises(TypeError): validate_noise_probabilities(['a', 'b', 'c']) with pytest.raises(ValueError): validate_noise_probabilities([0.0, 0.0, 0.0, 0.0]) with pytest.raises(ValueError): validate_noise_probabilities([0.5, 0.5, 0.5]) with pytest.raises(ValueError): validate_noise_probabilities([-0.5, -0.5, -0.5])
def test_validate_noise_probabilities(): with pytest.raises(TypeError): validate_noise_probabilities(1) with pytest.raises(TypeError): validate_noise_probabilities(["a", "b", "c"]) with pytest.raises(ValueError): validate_noise_probabilities([0.0, 0.0, 0.0, 0.0]) with pytest.raises(ValueError): validate_noise_probabilities([0.5, 0.5, 0.5]) with pytest.raises(ValueError): validate_noise_probabilities([-0.5, -0.5, -0.5])
def __init__(self, connection: ForestConnection, *args, noise_model=None, gate_noise=None, measurement_noise=None, random_seed=None, **kwargs): """ A virtual machine that classically emulates the execution of Quil programs. :param connection: A connection to the Forest web API. :param noise_model: A noise model that describes noise to apply when emulating a program's execution. :param gate_noise: A list of three numbers [Px, Py, Pz] indicating the probability of an X, Y, or Z gate getting applied to each qubit after a gate application or reset. The default value of None indicates no noise. :param measurement_noise: A list of three numbers [Px, Py, Pz] indicating the probability of an X, Y, or Z gate getting applied before a measurement. The default value of None indicates no noise. :param random_seed: A seed for the QVM's random number generators. Either None (for an automatically generated seed) or a non-negative integer. """ super().__init__(*args, **kwargs) if (noise_model is not None) and (gate_noise is not None or measurement_noise is not None): raise ValueError(""" You have attempted to supply the QVM with both a Kraus noise model (by supplying a `noise_model` argument), as well as either `gate_noise` or `measurement_noise`. At this time, only one may be supplied. To read more about supplying noise to the QVM, see http://pyquil.readthedocs.io/en/latest/noise_models.html#support-for-noisy-gates-on-the-rigetti-qvm. """) self.noise_model = noise_model self.connection = connection validate_noise_probabilities(gate_noise) validate_noise_probabilities(measurement_noise) self.gate_noise = gate_noise self.measurement_noise = measurement_noise if random_seed is None: self.random_seed = None elif isinstance(random_seed, integer_types) and random_seed >= 0: self.random_seed = random_seed else: raise TypeError("random_seed should be None or a non-negative int")
def __init__(self, device=None, endpoint=None, gate_noise=None, measurement_noise=None, random_seed=None, compiler_endpoint=None): """ Constructor for QVMConnection. Sets up any necessary security, and establishes the noise model to use. :param Device device: The optional device, from which noise will be added by default to all programs run on this instance. :param endpoint: The endpoint of the server for running small jobs :param gate_noise: A list of three numbers [Px, Py, Pz] indicating the probability of an X, Y, or Z gate getting applied to each qubit after a gate application or reset. (default None) :param measurement_noise: A list of three numbers [Px, Py, Pz] indicating the probability of an X, Y, or Z gate getting applied before a a measurement. (default None) :param random_seed: A seed for the QVM's random number generators. Either None (for an automatically generated seed) or a non-negative integer. """ if endpoint is None: pyquil_config = PyquilConfig() endpoint = pyquil_config.qvm_url if compiler_endpoint is None: pyquil_config = PyquilConfig() compiler_endpoint = pyquil_config.compiler_url if (device is not None and device.noise_model is not None) and \ (gate_noise is not None or measurement_noise is not None): raise ValueError(""" You have attempted to supply the QVM with both a device noise model (by having supplied a device argument), as well as either gate_noise or measurement_noise. At this time, only one may be supplied. To read more about supplying noise to the QVM, see http://pyquil.readthedocs.io/en/latest/noise_models.html#support-for-noisy-gates-on-the-rigetti-qvm. """) if device is not None and device.noise_model is None: warnings.warn(""" You have supplied the QVM with a device that does not have a noise model. No noise will be added to programs run on this QVM. """) self.noise_model = device.noise_model if device else None self.compiler = LocalQVMCompiler(endpoint=compiler_endpoint, device=device) if device \ else None self.sync_endpoint = endpoint validate_noise_probabilities(gate_noise) validate_noise_probabilities(measurement_noise) self.gate_noise = gate_noise self.measurement_noise = measurement_noise if random_seed is None: self.random_seed = None elif isinstance(random_seed, integer_types) and random_seed >= 0: self.random_seed = random_seed else: raise TypeError("random_seed should be None or a non-negative int") self._connection = ForestConnection(sync_endpoint=endpoint) self.session = self._connection.session # backwards compatibility