def test_depolarizer_different_gate(): q1 = cirq.QubitId() q2 = cirq.QubitId() cnot = Job(cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), ])) allerrors = DepolarizerChannel(probability=1.0, depolarizing_gates= [xmon_gates.ExpZGate(), xmon_gates.ExpWGate()]) p0 = cirq.Symbol(DepolarizerChannel._parameter_name + '0') p1 = cirq.Symbol(DepolarizerChannel._parameter_name + '1') p2 = cirq.Symbol(DepolarizerChannel._parameter_name + '2') p3 = cirq.Symbol(DepolarizerChannel._parameter_name + '3') error_sweep = (cirq.Points(p0.name, [1.0]) + cirq.Points(p1.name, [1.0]) + cirq.Points(p2.name, [1.0]) + cirq.Points(p3.name, [1.0])) cnot_then_z = Job( cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), cirq.Moment([xmon_gates.ExpZGate(half_turns=p0).on(q1), xmon_gates.ExpZGate(half_turns=p1).on(q2)]), cirq.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 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_depolarizer_no_errors(): q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') cnot = Job(cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), ])) no_errors = DepolarizerChannel(probability=0.0) assert no_errors.transform_job(cnot) == cnot
def test_depolarizer_no_errors(): q1 = ops.QubitId() q2 = ops.QubitId() cnot = Job(circuits.Circuit([ circuits.Moment([ops.CNOT(q1, q2)]), ])) noerrors = DepolarizerChannel(probability=0.0) assert noerrors.transform_job(cnot) == cnot
def test_depolarizer_multiple_realizations(): q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') cnot = Job(cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), ])) all_errors3 = DepolarizerChannel(probability=1.0, realizations=3) p0 = cirq.Symbol(DepolarizerChannel._parameter_name + '0') p1 = cirq.Symbol(DepolarizerChannel._parameter_name + '1') error_sweep = (cirq.Points(p0.name, [1.0, 1.0, 1.0]) + cirq.Points(p1.name, [1.0, 1.0, 1.0])) cnot_then_z3 = Job( cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), cirq.Moment([cirq.Z(q1)**p0, cirq.Z(q2)**p1]) ]), cnot.sweep * error_sweep) assert all_errors3.transform_job(cnot) == cnot_then_z3
def test_depolarizer_parameterized_gates(): q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') cnot_param = cirq.Symbol('cnot_turns') cnot_gate = cirq.CZ(q1, q2)**cnot_param job_sweep = cirq.Points('cnot_turns', [0.5]) cnot = Job(cirq.Circuit([cirq.Moment([cnot_gate])]), job_sweep) all_errors = DepolarizerChannel(probability=1.0) p0 = cirq.Symbol(DepolarizerChannel._parameter_name + '0') p1 = cirq.Symbol(DepolarizerChannel._parameter_name + '1') error_sweep = cirq.Points(p0.name, [1.0]) + cirq.Points(p1.name, [1.0]) cnot_then_z = Job( cirq.Circuit([ cirq.Moment([cnot_gate]), cirq.Moment([cirq.Z(q1)**p0, cirq.Z(q2)**p1]) ]), job_sweep * error_sweep) assert all_errors.transform_job(cnot) == cnot_then_z
def test_depolarizer_multiple_realizations(): q1 = cirq.QubitId() q2 = cirq.QubitId() cnot = Job(cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), ])) all_errors3 = DepolarizerChannel(probability=1.0, realizations=3) p0 = cirq.Symbol(DepolarizerChannel._parameter_name + '0') p1 = cirq.Symbol(DepolarizerChannel._parameter_name + '1') error_sweep = (cirq.Points(p0.name, [1.0, 1.0, 1.0]) + cirq.Points(p1.name, [1.0, 1.0, 1.0])) cnot_then_z3 = Job( cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), cirq.Moment([xmon_gates.ExpZGate(half_turns=p0).on(q1), xmon_gates.ExpZGate(half_turns=p1).on(q2)]) ]), cnot.sweep * error_sweep) assert all_errors3.transform_job(cnot) == cnot_then_z3
def test_depolarizer_parameterized_gates(): q1 = cirq.QubitId() q2 = cirq.QubitId() cnot_param = cirq.Symbol('cnot_turns') cnot_gate = xmon_gates.Exp11Gate(half_turns=cnot_param).on(q1, q2) job_sweep = cirq.Points('cnot_turns', [0.5]) cnot = Job(cirq.Circuit([cirq.Moment([cnot_gate])]), job_sweep) all_errors = DepolarizerChannel(probability=1.0) p0 = cirq.Symbol(DepolarizerChannel._parameter_name + '0') p1 = cirq.Symbol(DepolarizerChannel._parameter_name + '1') error_sweep = cirq.Points(p0.name, [1.0]) + cirq.Points(p1.name, [1.0]) cnot_then_z = Job( cirq.Circuit([ cirq.Moment([cnot_gate]), cirq.Moment([xmon_gates.ExpZGate(half_turns=p0).on(q1), xmon_gates.ExpZGate(half_turns=p1).on(q2)]) ]), job_sweep * error_sweep) assert all_errors.transform_job(cnot) == cnot_then_z
def test_depolarizer_all_errors(): q1 = ops.QubitId() q2 = ops.QubitId() cnot = Job(circuits.Circuit([ circuits.Moment([ops.CNOT(q1, q2)]), ])) 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([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 allerrors.transform_job(cnot) == cnot_then_z
def test_depolarizer_different_gate(): q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') cnot = Job(cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), ])) allerrors = DepolarizerChannel(probability=1.0, depolarizing_gates=[cirq.Z, cirq.X]) p0 = cirq.Symbol(DepolarizerChannel._parameter_name + '0') p1 = cirq.Symbol(DepolarizerChannel._parameter_name + '1') p2 = cirq.Symbol(DepolarizerChannel._parameter_name + '2') p3 = cirq.Symbol(DepolarizerChannel._parameter_name + '3') error_sweep = (cirq.Points(p0.name, [1.0]) + cirq.Points(p1.name, [1.0]) + cirq.Points(p2.name, [1.0]) + cirq.Points(p3.name, [1.0])) cnot_then_z = Job( cirq.Circuit([ cirq.Moment([cirq.CNOT(q1, q2)]), cirq.Moment([cirq.Z(q1)**p0, cirq.Z(q2)**p1]), cirq.Moment([cirq.X(q1)**p2, cirq.X(q2)**p3]) ]), cnot.sweep * error_sweep) assert allerrors.transform_job(cnot) == cnot_then_z
def test_job_equality(): eq = EqualsTester() q = ops.QubitId() q2 = ops.QubitId() # Equivalent empty jobs eq.add_equality_group(Job(), Job(Circuit()), Job(Circuit([])), Job(Circuit(), sweeps.Unit)) # Equivalent circuit, different instances eq.add_equality_group(Job(Circuit([Moment([ops.Z(q)])])), Job(Circuit([Moment([ops.Z(q)])]))) # Different Circuit c = Circuit([Moment([ops.CZ(q, q2)])]) eq.add_equality_group(Job(c)) ps1 = sweeps.Points('Example', [42.0]) ps2 = sweeps.Points('Example', [42.0]) ps3 = sweeps.Points('Example', [42.0, 1.4]) eq.add_equality_group(Job(c, ps1, 2), Job(c, ps2, 2)) eq.add_equality_group(Job(c, ps1, 4)) eq.add_equality_group(Job(c, ps3, 2))
def test_job_equality(): eq = cirq.testing.EqualsTester() q = cirq.QubitId() q2 = cirq.QubitId() # Equivalent empty jobs eq.add_equality_group(Job(), Job(cirq.Circuit()), Job(cirq.Circuit([])), Job(cirq.Circuit(), cirq.UnitSweep)) # Equivalent circuit, different instances eq.add_equality_group(Job(cirq.Circuit([cirq.Moment([cirq.Z(q)])])), Job(cirq.Circuit([cirq.Moment([cirq.Z(q)])]))) # Different Circuit c = cirq.Circuit([cirq.Moment([cirq.CZ(q, q2)])]) eq.add_equality_group(Job(c)) ps1 = cirq.Points('Example', [42.0]) ps2 = cirq.Points('Example', [42.0]) ps3 = cirq.Points('Example', [42.0, 1.4]) eq.add_equality_group(Job(c, ps1, 2), Job(c, ps2, 2)) eq.add_equality_group(Job(c, ps1, 4)) eq.add_equality_group(Job(c, ps3, 2))