def test_sample_from_wavefunction_fails_for_invalid_n_samples(n_samples):
    n_qubits = 4
    amplitudes = [0] * (2**n_qubits)
    amplitudes[1] = 1
    wavefunction = Wavefunction(amplitudes)
    with pytest.raises(AssertionError):
        sample_from_wavefunction(wavefunction, n_samples)
Example #2
0
 def run_circuit_and_measure(self, circuit, **kwargs):
     """
     Method for executing the circuit and measuring the outcome.
     Args:
         circuit (core.circuit.Circuit): quantum circuit to be executed.
     """
     wavefunction = self.get_wavefunction(circuit)
     return sample_from_wavefunction(wavefunction, self.n_samples)
def test_sample_from_wavefunction_list():
    n_qubits = 4
    expected_bitstring = (0, 0, 0, 1)
    amplitudes = [0] * (2**n_qubits)
    amplitudes[1] = 1  # |0001> will be measured in all cases.
    wavefunction = Wavefunction(amplitudes)
    sample = set(sample_from_wavefunction(wavefunction, 500))
    assert len(sample) == 1
    assert sample.pop() == expected_bitstring
def test_sample_from_wavefunction_column_vector():
    n_qubits = 4
    expected_bitstring = (0, 0, 0, 1)
    amplitudes = np.array([0] * (2**n_qubits)).reshape(2**n_qubits, 1)
    amplitudes[1] = 1  # |0001> will be measured in all cases.
    wavefunction = Wavefunction(amplitudes)
    sample = set(sample_from_wavefunction(wavefunction, 500))
    assert len(sample) == 1
    assert sample.pop() == expected_bitstring
def test_sample_from_wavefunction_list():
    n_qubits = 4
    # NOTE: our indexing places the state of qubit i at the ith index of the tuple. Hence |01> will result in
    # the tuple (1, 0)
    expected_bitstring = (1, 0, 0, 0)
    amplitudes = [0] * (2**n_qubits)
    amplitudes[1] = 1  # |0001> will be measured in all cases.
    wavefunction = Wavefunction(amplitudes)
    sample = set(sample_from_wavefunction(wavefunction, 500))
    assert len(sample) == 1
    assert sample.pop() == expected_bitstring
Example #6
0
    def run_circuit_and_measure(self, circuit, **kwargs):
        """
        Run a circuit and measure a certain number of bitstrings

        Args:
            circuit (zquantum.core.circuit.Circuit): the circuit to prepare the state
            n_samples (int): the number of bitstrings to sample
        Returns:
            a list of bitstrings (a list of tuples)
        """
        wavefunction = self.get_wavefunction(circuit)
        bitstrings = sample_from_wavefunction(wavefunction, self.n_samples)
        return Measurements(bitstrings)
def test_sample_from_wavefunction():
    wavefunction = create_random_wavefunction(4)

    samples = sample_from_wavefunction(wavefunction, 10000)
    sampled_dict = Counter(samples)

    sampled_probabilities = []
    for num in range(len(wavefunction)):
        bitstring = format(num, "b")
        while len(bitstring) < wavefunction.n_qubits:
            bitstring = "0" + bitstring
        measurement = convert_bitstrings_to_tuples([bitstring])[0]
        sampled_probabilities.append(sampled_dict[measurement] / 10000)

    probabilities = wavefunction.probabilities()
    for sampled_prob, exact_prob in zip(sampled_probabilities, probabilities):
        assert np.allclose(sampled_prob, exact_prob, atol=0.01)
Example #8
0
    def run_circuit_and_measure(self,
                                circuit: Circuit,
                                n_samples: Optional[int] = None,
                                **kwargs):
        """
        Run a circuit and measure a certain number of bitstrings

        Args:
            circuit: the circuit to prepare the state
            n_samples: the number of bitstrings to sample
        Returns:
            The measured bitstrings.
        """
        if n_samples is None:
            n_samples = self.n_samples
        wavefunction = self.get_wavefunction(circuit)
        bitstrings = sample_from_wavefunction(wavefunction, n_samples)
        return Measurements(bitstrings)
def test_sample_from_wavefunction():
    wavefunction = create_random_wavefunction(4)

    samples = sample_from_wavefunction(wavefunction, 10000)
    sampled_dict = Counter(samples)

    sampled_probabilities = []
    for num in range(len(wavefunction)**2):
        bitstring = format(num, "b")
        while len(bitstring) < len(wavefunction):
            bitstring = "0" + bitstring
        # NOTE: our indexing places the state of qubit i at the ith index of the tuple. Hence |01> will result in
        # the tuple (1, 0)
        bitstring = bitstring[::-1]
        measurement = convert_bitstrings_to_tuples([bitstring])[0]
        sampled_probabilities.append(sampled_dict[measurement] / 10000)

    probabilities = wavefunction.probabilities()
    for sampled_prob, exact_prob in zip(sampled_probabilities, probabilities):
        assert np.allclose(sampled_prob, exact_prob, atol=0.01)
Example #10
0
    def run_circuit_and_measure(
        self,
        circuit: Circuit,
        n_samples: int,
        symbol_map: Optional[Dict[Symbol, Any]] = {},
    ) -> Measurements:
        """Run a circuit and measure a certain number of bitstrings

        Args:
            circuit: the circuit to prepare the state
            n_samples: the number of bitstrings to sample
        """
        wavefunction = self.get_wavefunction(circuit).bind(
            symbol_map=symbol_map)

        if circuit.free_symbols:
            raise ValueError(
                "Cannot sample from circuit with symbolic parameters.")

        bitstrings = sample_from_wavefunction(wavefunction, n_samples,
                                              self._seed)
        return Measurements(bitstrings)
Example #11
0
    def run_circuit_and_measure(self,
                                circuit: Circuit,
                                n_samples: Optional[int] = None,
                                **kwargs):
        """
        Run a circuit and measure a certain number of bitstrings

        Args:
            circuit: the circuit to prepare the state
            n_samples: the number of bitstrings to sample
        Returns:
            The measured bitstrings.
        """
        if n_samples is None:
            if self.n_samples is None:
                raise ValueError(
                    "n_samples needs to be specified either as backend attribute or as an function argument."
                )
            else:
                n_samples = self.n_samples
        wavefunction = self.get_wavefunction(circuit)
        bitstrings = sample_from_wavefunction(wavefunction, n_samples)
        return Measurements(bitstrings)
Example #12
0
 def run_circuit_and_measure(self, circuit, **kwargs):
     wavefunction = self.get_wavefunction(circuit)
     return Measurements(
         sample_from_wavefunction(wavefunction, self.n_samples))