<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Copyright 2021 The Cirq Developers
Step1:  Quantum circuits on Rigetti devices
<table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https
Step2:  Running this notebook requires the pyQuil QVM and Compiler. If you are running on Google Colab or a Linux Debian machine, you can run the below cell to install them if necessary. If you are on a non-Linux Debian machine, see these instructions{
Step3:  Next, run the pyQuil QVM and Compiler if they are not already running on their default ports.
Step4:  Running a Bell state circuit
To demonstrate the basic functionality of the Cirq Rigetti integration, we begin constructing a basic Bell state circuit.
Step5:  Next, we'll import RigettiQCSService and list available quantum processors.
Step6:  For now, we'll instantiate the RigettiQCSService as a pyQuil Quantum Virtual Machine{
Step7:  We'll use the built-in cirq.plot_state_histogram to visually verify the results of our Bell state.
Step8:  As expected, we see states 0 (ie '00') and 3 (ie '11') as the dominant results.
You may initialize both the RigettiQCSService and RigettiQCSSampler with execution functions from the cirq_rigetti.circuit_sweep_executor module and transformation functions from cirq_rigetti.circuit_transformations.
You may invoke these functions with arguments for controlling your circuit execution at a more fine grained level. For instance, you may want add Pragma statements{
Step9:  Running a parameterized circuit
Of course, you may be running a parameterized circuit and would like to leverage the Quil compilers{
Step10:  Next, we will initialize a parameterized circuit in Cirq along with a set of parameter values.
Step11:  In much the same way that we invoke, RigettiQCSSerivce.run, we invoke RigettiQCSSampler.run_sweep with our parameters here.
<ASSISTANT_TASK:>
Python Code:
#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Explanation: Copyright 2021 The Cirq Developers
End of explanation
try:
    import cirq
except ImportError:
    print("installing cirq...")
    !pip install --quiet cirq
    print("installed cirq.")
    import cirq
Explanation: Quantum circuits on Rigetti devices
<table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https://quantumai.google/cirq/tutorials/rigetti/getting_started"><img src="https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png" />View on QuantumAI</a>
  </td>
  <td>
    <a target="_blank" href="https://colab.research.google.com/github/quantumlib/Cirq/blob/master/docs/tutorials/rigetti/getting_started.ipynb"><img src="https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png" />Run in Google Colab</a>
  </td>
  <td>
    <a target="_blank" href="https://github.com/quantumlib/Cirq/blob/master/docs/tutorials/rigetti/getting_started.ipynb"><img src="https://quantumai.google/site-assets/images/buttons/github_logo_1x.png" />View source on GitHub</a>
  </td>
  <td>
    <a href="https://storage.googleapis.com/tensorflow_docs/Cirq/docs/tutorials/rigetti/getting_started.ipynb"><img src="https://quantumai.google/site-assets/images/buttons/download_icon_1x.png" />Download notebook</a>
  </td>
</table>
End of explanation
! [ -z "$(which qvm)" ] &&\
  apt update &&\
  apt install jq &&\
  export LATEST_FOREST_SDK_VERSION=$(curl -s https://downloads.rigetti.com/qcs-sdk/versions | jq -r '.versions[0].sdk') &&\
  curl -f https://downloads.rigetti.com/qcs-sdk/forest-sdk-$LATEST_FOREST_SDK_VERSION-linux-deb.tar.bz2 -o $PWD/forest-sdk-$LATEST_FOREST_SDK_VERSION-linux-deb.tar.bz2 &&\
  tar -xf forest-sdk-$LATEST_FOREST_SDK_VERSION-linux-deb.tar.bz2 &&\
  ./forest-sdk-$LATEST_FOREST_SDK_VERSION-linux-deb/forest-sdk-$LATEST_FOREST_SDK_VERSION-linux-deb.run &&\
  quilc --version &&\
  qvm --version
Explanation: Running this notebook requires the pyQuil QVM and Compiler. If you are running on Google Colab or a Linux Debian machine, you can run the below cell to install them if necessary. If you are on a non-Linux Debian machine, see these instructions{:.external} for installation.
End of explanation
import subprocess
subprocess.Popen(["qvm", "--quiet", "-S"])
subprocess.Popen(["quilc", "--quiet", "-R"])
Explanation: Next, run the pyQuil QVM and Compiler if they are not already running on their default ports.
End of explanation
bell_circuit = cirq.Circuit()
qubits = cirq.LineQubit.range(2)
bell_circuit.append(cirq.H(qubits[0]))
bell_circuit.append(cirq.CNOT(qubits[0], qubits[1]))
bell_circuit.append(cirq.measure(qubits[0], qubits[1], key='m'))
print(bell_circuit)
Explanation: Running a Bell state circuit
To demonstrate the basic functionality of the Cirq Rigetti integration, we begin constructing a basic Bell state circuit.
End of explanation
from cirq_rigetti import RigettiQCSService
quantum_processors = RigettiQCSService.list_quantum_processors().quantum_processors
processors_list = [quantum_processor.id for quantum_processor in quantum_processors]
print(processors_list)
Explanation: Next, we'll import RigettiQCSService and list available quantum processors.
End of explanation
from cirq_rigetti import circuit_transformers, circuit_sweep_executors, get_rigetti_qcs_service
SERVICE_NAME = processors_list[0]
print(SERVICE_NAME)
service = get_rigetti_qcs_service(SERVICE_NAME, as_qvm=True, noisy=False)
result = service.run(bell_circuit, repetitions=1000)
print(result.histogram(key='m'))
Explanation: For now, we'll instantiate the RigettiQCSService as a pyQuil Quantum Virtual Machine{:.external} based on the topology of one of the available Rigetti quantum processors. At the time of this writing, Aspen-11 is available.
Note, in addition to the quantum processors listed above, you can also instantiate the RigettiQCSService by naming an arbitrary virtual device the pyQuil QVM supports. See the documentation for pyquil get_qc{:.external} for more information.
End of explanation
cirq.plot_state_histogram(result.histogram(key='m'))    
Explanation: We'll use the built-in cirq.plot_state_histogram to visually verify the results of our Bell state.
End of explanation
from pyquil.quilbase import Reset, Pragma
def hook(program, measurement_id_map):
    program._instructions.insert(0, Reset())
    program._instructions.insert(1, Pragma('INITIAL_REWIRING', freeform_string='GREEDY'))
    print(program)
    return program, measurement_id_map
# assign qubits explicitly to hardware or virtual machine qubits.
qubit_id_map = {
   qubits[0]: 4,
   qubits[1]: 5,
}
executor = circuit_sweep_executors.with_quilc_compilation_and_cirq_parameter_resolution
transformer = circuit_transformers.build(qubit_id_map=qubit_id_map, qubits=qubits, post_transformation_hooks=[hook])
service = get_rigetti_qcs_service(SERVICE_NAME, as_qvm=True, executor=executor, transformer=transformer)
result = service.run(bell_circuit, repetitions=1000)
cirq.plot_state_histogram(result.histogram(key='m'))
Explanation: As expected, we see states 0 (ie '00') and 3 (ie '11') as the dominant results.
You may initialize both the RigettiQCSService and RigettiQCSSampler with execution functions from the cirq_rigetti.circuit_sweep_executor module and transformation functions from cirq_rigetti.circuit_transformations.
You may invoke these functions with arguments for controlling your circuit execution at a more fine grained level. For instance, you may want add Pragma statements{:.external} to set the initial rewiring{:.external} strategy, invoke active qubit reset{:.external} prior to execution, or explicitly address physical qubits on the quantum computer.
End of explanation
from cirq_rigetti import get_rigetti_qcs_sampler
executor = circuit_sweep_executors.with_quilc_parametric_compilation
sampler = get_rigetti_qcs_sampler(SERVICE_NAME, as_qvm=True, executor=executor)
Explanation: Running a parameterized circuit
Of course, you may be running a parameterized circuit and would like to leverage the Quil compilers{:.external} support for parametric compilation. This affords a speedup in execution times as the Cirq Rigetti integration will only compile the circuit once for a single parameter sweep.
We start by initializing the RigettiQCSSampler and specifying a circuit sweep executor that supports parametric compilation. Note, that this class accepts the same executor and transformer types as RigettiQCSService.
End of explanation
import sympy
qubit = cirq.LineQubit.range(1)[0]
circuit = cirq.Circuit(
    cirq.H(qubit)**sympy.Symbol('t'),
    cirq.measure(qubit, key='m'))
params = cirq.Linspace('t', start=0, stop=4, length=5)
print(circuit)
Explanation: Next, we will initialize a parameterized circuit in Cirq along with a set of parameter values.
End of explanation
import pandas as pd
results = sampler.run_sweep(circuit, params, repetitions=10)
data = {f't={result.params["t"]}': [measurement[0] for measurement in result.measurements['m']] for result in results}
pd.DataFrame(data)
Explanation: In much the same way that we invoke, RigettiQCSSerivce.run, we invoke RigettiQCSSampler.run_sweep with our parameters here.
End of explanation
<END_TASK>