def test_depolarizer_different_gate(): q1 = ops.QubitId() q2 = ops.QubitId() cnot = Job(circuits.Circuit([ circuits.Moment([ops.CNOT(q1, q2)]), ])) allerrors = DepolarizerChannel( probability=1.0, depolarizing_gates=[xmon_gates.ExpZGate(), xmon_gates.ExpWGate()]) p0 = Symbol(DepolarizerChannel._parameter_name + '0') p1 = Symbol(DepolarizerChannel._parameter_name + '1') p2 = Symbol(DepolarizerChannel._parameter_name + '2') p3 = Symbol(DepolarizerChannel._parameter_name + '3') error_sweep = (Points(p0.name, [1.0]) + Points(p1.name, [1.0]) + Points(p2.name, [1.0]) + Points(p3.name, [1.0])) cnot_then_z = Job( circuits.Circuit([ circuits.Moment([ops.CNOT(q1, q2)]), circuits.Moment([ xmon_gates.ExpZGate(half_turns=p0).on(q1), xmon_gates.ExpZGate(half_turns=p1).on(q2) ]), circuits.Moment([ xmon_gates.ExpWGate(half_turns=p2).on(q1), xmon_gates.ExpWGate(half_turns=p3).on(q2) ]) ]), cnot.sweep * error_sweep) assert allerrors.transform_job(cnot) == cnot_then_z
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_zip(): sweep = ZipSweep() s1 = sweep.sweeps.add() s1.parameter_key = 'foo' s1.points.points.extend([1, 2, 3]) s2 = sweep.sweeps.add() s2.parameter_key = 'bar' s2.points.points.extend([4, 5]) out = params._sweep_from_param_sweep_zip(sweep) assert out == Points('foo', [1, 2, 3]) + 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 test_gen_sweep_points(): points = [0.5, 1.0, 1.5, 2.0, 2.5] sweep = SingleSweep() sweep.parameter_key = 'foo' sweep.points.points.extend(points) out = params._sweep_from_single_param_sweep(sweep) assert out == Points('foo', [0.5, 1.0, 1.5, 2.0, 2.5])
def test_gen_param_sweep_zip(): s1 = { 'parameter_key': 'foo', 'points': { 'points': [1, 2, 3] } } s2 = { 'parameter_key': 'bar', 'points': { 'points': [4, 5] } } sweep = { 'sweeps': [s1, s2] } out = params._sweep_from_param_sweep_zip_proto_dict(sweep) assert out == Points('foo', [1, 2, 3]) + Points('bar', [4, 5])
def test_gen_sweep_points(): points = [0.5, 1.0, 1.5, 2.0, 2.5] sweep = { 'parameter_key': 'foo', 'points': { 'points': list(points) } } out = params._sweep_from_single_param_sweep_proto_dict(sweep) assert out == Points('foo', [0.5, 1.0, 1.5, 2.0, 2.5])
def test_equality(): et = EqualsTester() et.add_equality_group(Unit, Unit) # Simple sweeps with the same key are equal to themselves, but different # from each other even if they happen to contain the same points. et.make_equality_group(lambda: Linspace('a', 0, 10, 11)) et.make_equality_group(lambda: Linspace('b', 0, 10, 11)) et.make_equality_group(lambda: Points('a', list(range(11)))) et.make_equality_group(lambda: Points('b', list(range(11)))) # Product and Zip sweeps can also be equated. et.make_equality_group( lambda: Linspace('a', 0, 5, 6) * Linspace('b', 10, 15, 6)) et.make_equality_group( lambda: Linspace('a', 0, 5, 6) + Linspace('b', 10, 15, 6)) et.make_equality_group(lambda: Points('a', [1, 2]) * (Linspace('b', 0, 5, 6) + Linspace('c', 10, 15, 6)))
def _sweep_from_single_param_sweep_proto_dict( single_param_sweep: Dict) -> Sweep: key = single_param_sweep['parameter_key'] if 'points' in single_param_sweep: points = single_param_sweep['points'] return Points(key, list(points['points'])) elif 'linspace' in single_param_sweep: sl = single_param_sweep['linspace'] return Linspace(key, sl['first_point'], sl['last_point'], sl['num_points']) else: raise ValueError('Single param sweep type undefined')
def _sweep_from_single_param_sweep( single_param_sweep: params_pb2.SingleSweep) -> Sweep: key = single_param_sweep.parameter_key which = single_param_sweep.WhichOneof('sweep') if which == 'points': sp = single_param_sweep.points return Points(key, list(sp.points)) elif which == 'linspace': sl = single_param_sweep.linspace return Linspace(key, sl.first_point, sl.last_point, sl.num_points) else: raise ValueError('unknown single param sweep type: {}'.format(which))
def test_depolarizer_multiple_realizations(): q1 = ops.QubitId() q2 = ops.QubitId() cnot = Job(circuits.Circuit([ circuits.Moment([ops.CNOT(q1, q2)]), ])) allerrors3 = DepolarizerChannel(probability=1.0, realizations=3) p0 = Symbol(DepolarizerChannel._parameter_name + '0') p1 = Symbol(DepolarizerChannel._parameter_name + '1') error_sweep = (Points(p0.name, [1.0, 1.0, 1.0]) + Points(p1.name, [1.0, 1.0, 1.0])) cnot_then_z3 = Job( circuits.Circuit([ circuits.Moment([ops.CNOT(q1, q2)]), circuits.Moment([ xmon_gates.ExpZGate(half_turns=p0).on(q1), xmon_gates.ExpZGate(half_turns=p1).on(q2) ]) ]), cnot.sweep * error_sweep) assert allerrors3.transform_job(cnot) == cnot_then_z3
def test_depolarizer_parameterized_gates(): q1 = ops.QubitId() q2 = ops.QubitId() cnot_param = Symbol('cnot_turns') cnot_gate = xmon_gates.Exp11Gate(half_turns=cnot_param).on(q1, q2) job_sweep = Points('cnot_turns', [0.5]) cnot = Job(circuits.Circuit([circuits.Moment([cnot_gate])]), job_sweep) allerrors = DepolarizerChannel(probability=1.0) p0 = Symbol(DepolarizerChannel._parameter_name + '0') p1 = Symbol(DepolarizerChannel._parameter_name + '1') error_sweep = Points(p0.name, [1.0]) + Points(p1.name, [1.0]) cnot_then_z = Job( circuits.Circuit([ circuits.Moment([cnot_gate]), circuits.Moment([ xmon_gates.ExpZGate(half_turns=p0).on(q1), xmon_gates.ExpZGate(half_turns=p1).on(q2) ]) ]), job_sweep * error_sweep) assert allerrors.transform_job(cnot) == cnot_then_z
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()])
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
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( Zip(Points('a', [1, 2, 3]), Points('b', [4, 5, 6])), Linspace('c', 0, 1, 5), ),
def test_points(): sweep = Points('a', [1, 2, 3, 4]) assert len(sweep) == 4 params = list(sweep) assert len(params) == 4
def test_product(): sweep = Points('a', [1, 2, 3]) * Points('b', [4, 5, 6, 7]) assert len(sweep) == 12 assert _values(sweep, 'a') == [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3] assert _values(sweep, 'b') == [4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7]
def test_zip(): sweep = Points('a', [1, 2, 3]) + Points('b', [4, 5, 6, 7]) assert len(sweep) == 3 assert _values(sweep, 'a') == [1, 2, 3] assert _values(sweep, 'b') == [4, 5, 6]