Example #1
0
 def _resolve_parameters_(
     self, param_resolver: cirq.ParamResolver
 ) -> "General1BitRotation":
     return General1BitRotation(
         rad1=param_resolver.value_of(self.rad1),
         rad2=param_resolver.value_of(self.rad2),
         rad3=param_resolver.value_of(self.rad3)
     )
Example #2
0
    def test_plugins_honeywell_cirq(self):
        with unittest.mock.patch.object(
                Job,
                self.mock_create_job_id_name,
                return_value=self.get_test_job_id(),
        ):
            workspace = self.create_workspace()
            service = AzureQuantumService(workspace=workspace)
            program = self._3_qubit_ghz_cirq()
            try:
                run_result = service.run(program=program,
                                         repetitions=500,
                                         target="honeywell.hqs-lt-s1-apival",
                                         timeout_seconds=60)

            except TimeoutError as e:
                # Pass on timeout
                warnings.warn("Honeywell execution exceeded timeout. \
                    Skipping fetching results.")
                if self.is_playback:
                    raise e

            except RuntimeError as e:
                # cirq_ionq currently throws a RuntimeError both if the job
                # failed and on timeout.
                # See: https://github.com/quantumlib/Cirq/issues/4507
                if 'Job failed' in str(e) or self.is_playback:
                    warnings.warn(f"Honeywell job execution failed: {str(e)}")
                    raise e
                else:
                    warnings.warn("Honeywell execution exceeded timeout. \
                    Skipping fetching results.")

            else:
                job_no_program = service.get_job(self.get_test_job_id())
                job_with_program = service.get_job(self.get_test_job_id(),
                                                   program=program)
                target = service._target_factory.create_target(
                    provider_id="honeywell", name="honeywell.hqs-lt-s1-apival")
                job_result1 = target._to_cirq_result(
                    result=job_no_program.results(),
                    param_resolver=ParamResolver({}))
                job_result2 = target._to_cirq_result(
                    result=job_with_program.results(),
                    param_resolver=ParamResolver({}))
                for result in [run_result, job_result1, job_result2]:
                    assert "q0" in result.measurements
                    assert "q1" in result.measurements
                    assert "q2" in result.measurements
                    assert len(result.measurements["q0"]) == 500
                    assert len(result.measurements["q1"]) == 500
                    assert len(result.measurements["q2"]) == 500
                    assert result.measurements["q0"].sum(
                    ) == result.measurements["q1"].sum()
                    assert result.measurements["q1"].sum(
                    ) == result.measurements["q2"].sum()
Example #3
0
def noisy_job(a, b, x, z, alpha, exact=False):
    """Adds noise to compression circuit.

    Args:
    =====
    a, b, x, z : numeric
        Circuit parameters for encoding circuit
    alpha : numeric
        Parameter for state preparation circuit
    exact : bool
        If True, works with wavefunction

    Returns:
    ========
    noisy_circuit : cirq.Circuit
        Noisy version of input circuit
    param_resolvers : list
    """
    job = Job(compression_circuit(a, b, x, z, alpha, exact))
    noisy = DepolarizerChannel(probability=noise_level)
    noisy_job = noisy.transform_job(job)
    param_resolvers = [
        ParamResolver({k: v
                       for k, v in e}) for e in noisy_job.sweep.param_tuples()
    ]
    return noisy_job.circuit, param_resolvers
Example #4
0
def resolve_parameters(
        val: Any, param_resolver: 'cirq.ParamResolverOrSimilarType') -> Any:
    """Resolves symbol parameters in the effect using the param resolver.

    This function will use the `_resolve_parameters_` magic method
    of `val` to resolve any Symbols with concrete values from the given
    parameter resolver.

    Args:
        val: The object to resolve (e.g. the gate, operation, etc)
        param_resolver: the object to use for resolving all symbols

    Returns:
        a gate or operation of the same type, but with all Symbols
        replaced with floats according to the given ParamResolver.
        If `val` has no `_resolve_parameters_` method or if it returns
        NotImplemented, `val` itself is returned.
    """
    if not param_resolver:
        return val

    # Ensure its a dictionary wrapped in a ParamResolver.
    from cirq import ParamResolver  # HACK: break cycle.
    param_resolver = ParamResolver(param_resolver)
    if isinstance(val, sympy.Basic):
        return param_resolver.value_of(val)

    getter = getattr(val, '_resolve_parameters_', None)
    result = NotImplemented if getter is None else getter(param_resolver)

    if result is not NotImplemented:
        return result
    else:
        return val
Example #5
0
 def _resolve_parameters_(
         self, param_resolver: cirq.ParamResolver) -> "PauliWordExpGate":
     return TwoPauliExpGate(
         pauli0=self.pauli0,
         pauli1=self.pauli1,
         rad=param_resolver.value_of(self.rad),
     )
Example #6
0
def add_noise(circuit, noise_level):
    """Adds depolarizing noise to circuit.

    Args:
    =====
    circuit : cirq.Circuit
        Circuit in which noise to be added
    noise_level : float
        Probability of a qubit being affected by the noise channel in a given moment

    Returns: 
    ========
    noisy_circuit : cirq.Circuit
        Noisy version of circuit
    """
    job = Job(circuit)
    noisy_channel = DepolarizerChannel(probability=noise_level)
    noisy_job = noisy_channel.transform_job(job)
    param_resolvers = [
        ParamResolver({k: v
                       for k, v in e}) for e in noisy_job.sweep.param_tuples()
    ]
    return noisy_job.circuit, param_resolvers
def noisy_job_stage3(aht, ht, zz, exact=False):
    """Adds noise to decoding circuit.

    Args:
    =====
    aht, ht, zz : numeric
        Circuit parameters for decoding circuit
    exact : bool
        If True, works with wavefunction

    Returns:
    ========
    noisy_circuit : cirq.Circuit
        Noisy version of input circuit
    param_resolvers : list
    """
    job = Job(decoder_circuit(aht, ht, zz, exact))
    noisy = DepolarizerChannel(probability=noise_level)
    noisy_job = noisy.transform_job(job)
    param_resolvers = [
        ParamResolver({k: v
                       for k, v in e}) for e in noisy_job.sweep.param_tuples()
    ]
    return noisy_job.circuit, param_resolvers
Example #8
0
 def _resolve_parameters_(
         self, param_resolver: cirq.ParamResolver) -> "GlobalPhaseGate":
     return GlobalPhaseGate(rad=param_resolver.value_of(self.rad))
Example #9
0
 def _resolve_parameters_(
         self, param_resolver: cirq.ParamResolver) -> "PauliWordExpGate":
     return PauliWordExpGate(coefficient=param_resolver.value_of(
         self.coefficient),
                             pauli_word=self.pauli_word)