def test_gen_param_sweep(): s1 = { 'parameter_key': 'foo', 'points': { 'points': [1, 2, 3] } } s2 = { 'parameter_key': 'bar', 'points': { 'points': [4, 5] } } ps = { 'sweep': { 'factors': [ { 'sweeps': [s1] }, { 'sweeps': [s2] } ] } } out = params.sweep_from_proto_dict(ps) assert out == Product(Zip(Points('foo', [1, 2, 3])), Zip(Points('bar', [4, 5])))
def test_gen_param_sweep(): ps = ParameterSweep() f1 = ps.sweep.factors.add() s1 = f1.sweeps.add() s1.parameter_key = 'foo' s1.points.points.extend([1, 2, 3]) f2 = ps.sweep.factors.add() s2 = f2.sweeps.add() s2.parameter_key = 'bar' s2.points.points.extend([4, 5]) out = params.sweep_from_proto(ps) assert out == Product(Zip(Points('foo', [1, 2, 3])), Zip(Points('bar', [4, 5])))
def transform_job(self, job): """Creates a new job object with depolarizing channel. This job will contain the existing Job's circuit with an error gate per qubit at every moment. Creates the parameter sweep for each gate and populates with random values as per the specifications of the depolarizer channel. Args: job: Job object to transform Returns: A new Job object that contains a circuit with up to double the moments as the original job, with every other moment being a moment containing error gates. It will also contain a Sweep containing values for each error gate. Note that moments that contain no error gates for any repetition will be automatically omitted. """ # A set for quick lookup of pre-existing qubits qubit_set = set() # A list with deterministic qubit order qubit_list = [] circuit = job.circuit # Retrieve the set of qubits used in the circuit for moment in circuit: for op in moment.operations: for qubit in op.qubits: if qubit not in qubit_set: qubit_set.add(qubit) qubit_list.append(qubit) # Add error circuits moments = [] error_number = 0 error_sweep = Zip() for moment in circuit: moments.append(moment) for gate in self.depolarizing_gates: error_gates = [] for q in qubit_list: errors = np.random.random(self.realizations) < self.p if any(errors): key = self._parameter_name + str(error_number) new_error_gate = gate**Symbol(key) error_gates.append(new_error_gate.on(q)) error_sweep += Points(key, list(errors * 1.0)) error_number += 1 if error_gates: moments.append(ops.Moment(error_gates)) sweep = job.sweep if error_sweep: sweep *= error_sweep return Job(Circuit(moments), sweep, job.repetitions)
def _sweep_from_param_sweep_zip_proto_dict(param_sweep_zip: Dict) -> Sweep: if 'sweeps' in param_sweep_zip: return Zip(*[ _sweep_from_single_param_sweep_proto_dict(sweep) for sweep in param_sweep_zip['sweeps'] ]) return UnitSweep
def _to_zip_product(sweep: Sweep) -> Product: """Converts sweep to a product of zips of single sweeps, if possible.""" if not isinstance(sweep, Product): sweep = Product(sweep) if not all(isinstance(f, Zip) for f in sweep.factors): factors = [f if isinstance(f, Zip) else Zip(f) for f in sweep.factors] sweep = Product(*factors) for factor in sweep.factors: for term in cast(Zip, factor).sweeps: if not isinstance(term, SingleSweep): raise ValueError( 'cannot convert to zip-product form: {}'.format(sweep)) return sweep
def _resolver_to_sweep(resolver: ParamResolver) -> Sweep: params = resolver.param_dict if not params: return UnitSweep return Zip( *[Points(key, [cast(float, value)]) for key, value in params.items()])
@pytest.mark.parametrize('param_sweep', example_sweeps()) def test_param_sweep_size_versus_gen(param_sweep): sweep = params.sweep_from_proto(param_sweep) predicted_size = len(sweep) out = list(sweep) assert len(out) == predicted_size @pytest.mark.parametrize('sweep,expected', [ ( Unit, Unit ), ( Linspace('a', 0, 10, 25), Product(Zip(Linspace('a', 0, 10, 25))) ), ( Points('a', [1, 2, 3]), Product(Zip(Points('a', [1, 2, 3]))) ), ( Zip(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3])), Product(Zip(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3]))), ), ( Product(Linspace('a', 0, 1, 5), Points('b', [1, 2, 3])), Product(Zip(Linspace('a', 0, 1, 5)), Zip(Points('b', [1, 2, 3]))), ), ( Product(
def _sweep_from_param_sweep_zip(param_sweep_zip: params_pb2.ZipSweep) -> Sweep: return Zip(*[ _sweep_from_single_param_sweep(sweep) for sweep in param_sweep_zip.sweeps ])
def _resolver_to_sweep(resolver: ParamResolver) -> Sweep: return Zip(*[Points(key, [value]) for key, value in resolver.param_dict.items()]) if len( resolver.param_dict) else UnitSweep