def test_readout_noise_after_moment(): program = cirq.Circuit() qubits = cirq.LineQubit.range(3) program.append([ cirq.H(qubits[0]), cirq.CNOT(qubits[0], qubits[1]), cirq.CNOT(qubits[1], qubits[2]) ]) program.append( [ cirq.measure(qubits[0], key='q0'), cirq.measure(qubits[1], key='q1'), cirq.measure(qubits[2], key='q2'), ], strategy=cirq.InsertStrategy.NEW_THEN_INLINE, ) # Use noise model to generate circuit depol_noise = ccn.DepolarizingNoiseModel(depol_prob=0.01) readout_noise = ccn.ReadoutNoiseModel(bitflip_prob=0.05) noisy_circuit = cirq.Circuit(depol_noise.noisy_moments(program, qubits)) noisy_circuit = cirq.Circuit( readout_noise.noisy_moments(noisy_circuit, qubits)) # Insert channels explicitly true_noisy_program = cirq.Circuit() true_noisy_program.append([cirq.H(qubits[0])]) true_noisy_program.append( [ cirq.DepolarizingChannel(0.01).on(q).with_tags(ops.VirtualTag()) for q in qubits ], strategy=cirq.InsertStrategy.NEW_THEN_INLINE, ) true_noisy_program.append([cirq.CNOT(qubits[0], qubits[1])]) true_noisy_program.append( [ cirq.DepolarizingChannel(0.01).on(q).with_tags(ops.VirtualTag()) for q in qubits ], strategy=cirq.InsertStrategy.NEW_THEN_INLINE, ) true_noisy_program.append([cirq.CNOT(qubits[1], qubits[2])]) true_noisy_program.append( [ cirq.DepolarizingChannel(0.01).on(q).with_tags(ops.VirtualTag()) for q in qubits ], strategy=cirq.InsertStrategy.NEW_THEN_INLINE, ) true_noisy_program.append([ cirq.BitFlipChannel(0.05).on(q).with_tags(ops.VirtualTag()) for q in qubits ]) true_noisy_program.append([ cirq.measure(qubits[0], key='q0'), cirq.measure(qubits[1], key='q1'), cirq.measure(qubits[2], key='q2'), ]) assert_equivalent_op_tree(true_noisy_program, noisy_circuit)
def test_per_qubit_combined_noise_from_data(): # Generate the combined noise model from calibration data. calibration = cirq_google.Calibration(_CALIBRATION_DATA) noise_model = simple_noise_from_calibration_metrics( calibration=calibration, depol_noise=True, readout_error_noise=True, readout_decay_noise=True, ) # Create the circuit and apply the noise model. qubits = [cirq.GridQubit(0, 0), cirq.GridQubit(0, 1), cirq.GridQubit(1, 0)] program = cirq.Circuit( cirq.Moment([cirq.H(qubits[0])]), cirq.Moment([cirq.CNOT(qubits[0], qubits[1])]), cirq.Moment([cirq.CNOT(qubits[0], qubits[2])]), cirq.Moment([ cirq.measure(qubits[0], key='q0'), cirq.measure(qubits[1], key='q1'), cirq.measure(qubits[2], key='q2'), ]), ) noisy_circuit = cirq.Circuit(noise_model.noisy_moments(program, qubits)) # Insert channels explicitly to construct expected output. decay_prob = [ 1 - exp(-1 / 0.007), 1 - exp(-1 / 0.008), 1 - exp(-1 / 0.009) ] expected_program = cirq.Circuit( cirq.Moment([cirq.H(qubits[0])]), cirq.Moment([cirq.DepolarizingChannel(DEPOL_001).on(qubits[0])]), cirq.Moment([cirq.CNOT(qubits[0], qubits[1])]), cirq.Moment([ cirq.DepolarizingChannel(DEPOL_001).on(qubits[0]), cirq.DepolarizingChannel(DEPOL_002).on(qubits[1]), ]), cirq.Moment([cirq.CNOT(qubits[0], qubits[2])]), cirq.Moment([ cirq.DepolarizingChannel(DEPOL_001).on(qubits[0]), cirq.DepolarizingChannel(DEPOL_003).on(qubits[2]), ]), cirq.Moment([ cirq.AmplitudeDampingChannel(decay_prob[i]).on(qubits[i]) for i in range(3) ]), cirq.Moment([ cirq.BitFlipChannel(0.004).on(qubits[0]), cirq.BitFlipChannel(0.005).on(qubits[1]), cirq.BitFlipChannel(0.006).on(qubits[2]), ]), cirq.Moment([ cirq.measure(qubits[0], key='q0'), cirq.measure(qubits[1], key='q1'), cirq.measure(qubits[2], key='q2'), ]), ) assert_equivalent_op_tree(expected_program, noisy_circuit)
def test_per_qubit_depol_noise_from_data(): # Generate the depolarization noise model from calibration data. calibration = cirq_google.Calibration(_CALIBRATION_DATA) noise_model = simple_noise_from_calibration_metrics(calibration=calibration, depol_noise=True) # Create the circuit and apply the noise model. qubits = [cirq.GridQubit(0, 0), cirq.GridQubit(0, 1), cirq.GridQubit(1, 0)] program = cirq.Circuit( cirq.Moment([cirq.H(qubits[0])]), cirq.Moment([cirq.CNOT(qubits[0], qubits[1])]), cirq.Moment([cirq.CNOT(qubits[0], qubits[2])]), cirq.Moment([cirq.Z(qubits[1]).with_tags(cirq.VirtualTag())]), cirq.Moment( [ cirq.measure(qubits[0], key='q0'), cirq.measure(qubits[1], key='q1'), cirq.measure(qubits[2], key='q2'), ] ), ) noisy_circuit = cirq.Circuit(noise_model.noisy_moments(program, qubits)) # Insert channels explicitly to construct expected output. expected_program = cirq.Circuit( cirq.Moment([cirq.H(qubits[0])]), cirq.Moment([cirq.DepolarizingChannel(DEPOL_001).on(qubits[0])]), cirq.Moment([cirq.CNOT(qubits[0], qubits[1])]), cirq.Moment( [ cirq.DepolarizingChannel(DEPOL_001).on(qubits[0]), cirq.DepolarizingChannel(DEPOL_002).on(qubits[1]), ] ), cirq.Moment([cirq.CNOT(qubits[0], qubits[2])]), cirq.Moment( [ cirq.DepolarizingChannel(DEPOL_001).on(qubits[0]), cirq.DepolarizingChannel(DEPOL_003).on(qubits[2]), ] ), cirq.Moment([cirq.Z(qubits[1]).with_tags(cirq.VirtualTag())]), cirq.Moment( [ cirq.measure(qubits[0], key='q0'), cirq.measure(qubits[1], key='q1'), cirq.measure(qubits[2], key='q2'), ] ), ) assert_equivalent_op_tree(expected_program, noisy_circuit)
def test_aggregate_decay_noise_after_moment(): program = cirq.Circuit() qubits = cirq.LineQubit.range(3) program.append([ cirq.H(qubits[0]), cirq.CNOT(qubits[0], qubits[1]), cirq.CNOT(qubits[1], qubits[2]) ]) program.append( [ cirq.measure(qubits[0], key='q0'), cirq.measure(qubits[1], key='q1'), cirq.measure(qubits[2], key='q2'), ], strategy=cirq.InsertStrategy.NEW_THEN_INLINE, ) # Use noise model to generate circuit noise_model = ccn.DepolarizingWithDampedReadoutNoiseModel( depol_prob=0.01, decay_prob=0.02, bitflip_prob=0.05) noisy_circuit = cirq.Circuit(noise_model.noisy_moments(program, qubits)) # Insert channels explicitly true_noisy_program = cirq.Circuit() true_noisy_program.append([cirq.H(qubits[0])]) true_noisy_program.append( [cirq.DepolarizingChannel(0.01).on(q) for q in qubits], strategy=cirq.InsertStrategy.NEW_THEN_INLINE, ) true_noisy_program.append([cirq.CNOT(qubits[0], qubits[1])]) true_noisy_program.append( [cirq.DepolarizingChannel(0.01).on(q) for q in qubits], strategy=cirq.InsertStrategy.NEW_THEN_INLINE, ) true_noisy_program.append([cirq.CNOT(qubits[1], qubits[2])]) true_noisy_program.append( [cirq.DepolarizingChannel(0.01).on(q) for q in qubits], strategy=cirq.InsertStrategy.NEW_THEN_INLINE, ) true_noisy_program.append( [cirq.AmplitudeDampingChannel(0.02).on(q) for q in qubits]) true_noisy_program.append( [cirq.BitFlipChannel(0.05).on(q) for q in qubits]) true_noisy_program.append([ cirq.measure(qubits[0], key='q0'), cirq.measure(qubits[1], key='q1'), cirq.measure(qubits[2], key='q2'), ]) assert_equivalent_op_tree(true_noisy_program, noisy_circuit)
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE: if self.is_virtual_moment(moment): return moment moments = [] if validate_all_measurements(moment): if self.decay_probs: moments.append( cirq.Moment( cirq.AmplitudeDampingChannel(self.decay_probs[q])(q) for q in system_qubits)) if self.bitflip_probs: moments.append( cirq.Moment( cirq.BitFlipChannel(self.bitflip_probs[q])(q) for q in system_qubits)) moments.append(moment) return moments else: moments.append(moment) if self.depol_probs: gated_qubits = [ q for q in system_qubits if moment.operates_on_single_qubit(q) ] if gated_qubits: moments.append( cirq.Moment( cirq.DepolarizingChannel(self.depol_probs[q])(q) for q in gated_qubits)) return moments
def test_supports_on_each_inheritance_shim(): class NotOnEach(cirq.Gate): def num_qubits(self): return 1 # coverage: ignore class OnEach(cirq.ops.gate_features.SupportsOnEachGate): def num_qubits(self): return 1 # coverage: ignore class SingleQ(cirq.SingleQubitGate): pass class TwoQ(cirq.TwoQubitGate): pass not_on_each = NotOnEach() single_q = SingleQ() two_q = TwoQ() with assert_deprecated(deadline="v0.14"): on_each = OnEach() assert not isinstance(not_on_each, cirq.ops.gate_features.SupportsOnEachGate) assert isinstance(on_each, cirq.ops.gate_features.SupportsOnEachGate) assert isinstance(single_q, cirq.ops.gate_features.SupportsOnEachGate) assert not isinstance(two_q, cirq.ops.gate_features.SupportsOnEachGate) assert isinstance(cirq.X, cirq.ops.gate_features.SupportsOnEachGate) assert not isinstance(cirq.CX, cirq.ops.gate_features.SupportsOnEachGate) assert isinstance(cirq.DepolarizingChannel(0.01), cirq.ops.gate_features.SupportsOnEachGate)
def test_svg_noise(): noise_model = cirq.ConstantQubitNoiseModel(cirq.DepolarizingChannel(p=1e-3)) q = cirq.LineQubit(0) circuit = cirq.Circuit(cirq.X(q)) circuit = cirq.Circuit(noise_model.noisy_moments(circuit.moments, [q])) svg = circuit_to_svg(circuit) assert '>D(0.001)</text>' in svg
def main(*, num_qubits: int, depth: int, num_circuits: int, seed: int, routes: int): """Run the quantum volume algorithm with a preset configuration. See the calculate_quantum_volume documentation for more details. Args: num_qubits: Pass-through to calculate_quantum_volume. depth: Pass-through to calculate_quantum_volume num_circuits: Pass-through to calculate_quantum_volume seed: Pass-through to calculate_quantum_volume Returns: Pass-through from calculate_quantum_volume. """ device = cirq.google.Bristlecone compiler = lambda circuit: cirq.google.optimized_for_xmon( circuit=circuit, new_device=device) noisy = cirq.DensityMatrixSimulator(noise=cirq.ConstantQubitNoiseModel( qubit_noise_gate=cirq.DepolarizingChannel(p=0.005))) calculate_quantum_volume(num_qubits=num_qubits, depth=depth, num_circuits=num_circuits, random_state=seed, device_or_qubits=device, samplers=[cirq.Simulator(), noisy], routing_attempts=routes, compiler=compiler)
def test_deprecated_on_each_for_depolarizing_channel_one_qubit(): q0 = cirq.LineQubit.range(1) op = cirq.DepolarizingChannel(p=0.1, n_qubits=1) op.on_each(q0) op.on_each([q0]) with pytest.raises(ValueError, match="Gate was called with type different than Qid"): op.on_each('bogus object')
def test_tensor_density_matrix_gridqubit(): qubits = cirq.GridQubit.rect(2, 2) circuit = cirq.testing.random_circuit(qubits=qubits, n_moments=10, op_density=0.8) circuit = cirq.drop_empty_moments(circuit) noise_model = cirq.ConstantQubitNoiseModel(cirq.DepolarizingChannel(p=1e-3)) circuit = cirq.Circuit(noise_model.noisy_moments(circuit.moments, qubits)) rho1 = cirq.final_density_matrix(circuit, dtype=np.complex128) rho2 = ccq.tensor_density_matrix(circuit, qubits) np.testing.assert_allclose(rho1, rho2, atol=1e-8)
def syc23_noisy(state): return qb.CirqBoard( state, sampler=cirq.DensityMatrixSimulator(noise=cirq.ConstantQubitNoiseModel( qubit_noise_gate=cirq.DepolarizingChannel(0.005)), seed=get_seed()), device=utils.get_device_obj_by_name('Syc23-simulator'), error_mitigation=enums.ErrorMitigation.Correct, noise_mitigation=0.10)
def test_tensor_density_matrix_4(): qubits = cirq.LineQubit.range(4) circuit = cirq.testing.random_circuit(qubits=qubits, n_moments=100, op_density=0.8) cirq.DropEmptyMoments().optimize_circuit(circuit) noise_model = cirq.ConstantQubitNoiseModel(cirq.DepolarizingChannel(p=1e-3)) circuit = cirq.Circuit(noise_model.noisy_moments(circuit.moments, qubits)) rho1 = cirq.final_density_matrix(circuit, dtype=np.complex128) rho2 = ccq.tensor_density_matrix(circuit, qubits) np.testing.assert_allclose(rho1, rho2, atol=1e-8)
def get_supported_channels(): """A helper to get the channels that are supported in TFQ. Returns a dictionary mapping from supported channel types to number of qubits. """ # Add new channels here whenever additional support is needed. channel_mapping = dict() channel_mapping[cirq.DepolarizingChannel(0.01)] = 1 channel_mapping[cirq.AsymmetricDepolarizingChannel(0.01, 0.02, 0.03)] = 1 return channel_mapping
def test_deprecated_on_each_for_depolarizing_channel_two_qubits(): q0, q1, q2, q3, q4, q5 = cirq.LineQubit.range(6) op = cirq.DepolarizingChannel(p=0.1, n_qubits=2) op.on_each([(q0, q1)]) op.on_each([(q0, q1), (q2, q3)]) op.on_each(zip([q0, q2, q4], [q1, q3, q5])) op.on_each((q0, q1)) op.on_each([q0, q1]) with pytest.raises(ValueError, match='Inputs to multi-qubit gates must be Sequence'): op.on_each(q0, q1) with pytest.raises(ValueError, match='All values in sequence should be Qids'): op.on_each([('bogus object 0', 'bogus object 1')]) with pytest.raises(ValueError, match='All values in sequence should be Qids'): op.on_each(['01']) with pytest.raises(ValueError, match='All values in sequence should be Qids'): op.on_each([(False, None)])
def __init__(self, depol_prob: float, bitflip_prob: float, decay_prob: float): self.qubit_noise_gate = cirq.DepolarizingChannel(depol_prob) self.readout_noise_gate = cirq.BitFlipChannel(bitflip_prob) self.readout_decay_gate = cirq.AmplitudeDampingChannel(decay_prob)
def stim_to_cirq_gate_table( ) -> Dict[str, Union[Tuple, cirq.Gate, Callable[[float], cirq.Gate]]]: return { "R": cirq.ResetChannel(), "I": cirq.I, "X": cirq.X, "Y": cirq.Y, "Z": cirq.Z, "H_XY": cirq.SingleQubitCliffordGate.from_xz_map(x_to=(cirq.Y, False), z_to=(cirq.Z, True)), "H": cirq.H, "H_YZ": cirq.SingleQubitCliffordGate.from_xz_map(x_to=(cirq.X, True), z_to=(cirq.Y, False)), "SQRT_X": cirq.X**0.5, "SQRT_X_DAG": cirq.X**-0.5, "SQRT_Y": cirq.Y**0.5, "SQRT_Y_DAG": cirq.Y**-0.5, "S": cirq.S, "S_DAG": cirq.S**-1, "SWAP": cirq.SWAP, "ISWAP": cirq.ISWAP, "ISWAP_DAG": cirq.ISWAP**-1, "XCX": cirq.PauliInteractionGate(cirq.X, False, cirq.X, False), "XCY": cirq.PauliInteractionGate(cirq.X, False, cirq.Y, False), "XCZ": cirq.PauliInteractionGate(cirq.X, False, cirq.Z, False), "YCX": cirq.PauliInteractionGate(cirq.Y, False, cirq.X, False), "YCY": cirq.PauliInteractionGate(cirq.Y, False, cirq.Y, False), "YCZ": cirq.PauliInteractionGate(cirq.Y, False, cirq.Z, False), "CX": cirq.CNOT, "CY": cirq.Y.controlled(1), "CZ": cirq.CZ, "DEPOLARIZE1": lambda arg: cirq.DepolarizingChannel(arg, 1), "DEPOLARIZE2": lambda arg: cirq.DepolarizingChannel(arg, 2), "X_ERROR": lambda arg: cirq.X.with_probability(arg), "Y_ERROR": lambda arg: cirq.Y.with_probability(arg), "Z_ERROR": lambda arg: cirq.Z.with_probability(arg), "DETECTOR": (), "OBSERVABLE_INCLUDE": (), "TICK": (), }
return input_qubits p=(0,0.02,0.04,0.06,0.08,0.1,0.12,0.14,0.16,0.18) qubit_count = 9 circuit_sample_count = 10 #Set up input and output qubits. input_qubits = set_io_qubits(qubit_count) phi = cirq.GridQubit(qubit_count + 1, 0) #gate = cirq.SingleQubitMatrixGate(matrix=np.array([[1, 0], [0, 1]])) estimate = [] for k in range(10): circuit = cirq.Circuit() circuit.append(cirq.H(q) for q in input_qubits) circuit.append(cirq.DepolarizingChannel(k/50).on_each(*input_qubits)) for i in range(qubit_count): gate = (cirq.SingleQubitMatrixGate(matrix=np.array([[np.exp(2j), 0], [0, 1]])))**2**i cgate = gate.controlled_by(input_qubits[qubit_count-1-i]) circuit.append(cgate(phi)) circuit.append(cirq.DepolarizingChannel(k/50).on_each(*input_qubits)) circuit.append(QftInverse(qubit_count)(*input_qubits)) circuit.append(cirq.DepolarizingChannel(k/50).on_each(*input_qubits)) # Measure the result. circuit.append(cirq.measure(*input_qubits, key='phase')) simulator = cirq.Simulator() result = simulator.run(circuit, repetitions=1000)
def test_deprecated_on_each_for_depolarizing_channel_two_qubits(): q0, q1 = cirq.LineQubit.range(2) op = cirq.DepolarizingChannel(p=0.1, n_qubits=2) with pytest.raises(ValueError, match="one qubit"): op.on_each(q0, q1)
def test_depolarizing_channel_repr_two_qubits(): cirq.testing.assert_equivalent_repr( cirq.DepolarizingChannel(0.3, n_qubits=2))
def test_depolarizing_channel_repr(): cirq.testing.assert_equivalent_repr(cirq.DepolarizingChannel(0.3))
), 'Syc23-noiseless': QuantumProcessor( name='Syc23-noiseless', device_obj=cg.Sycamore23, processor_id=None, is_simulator=True, _get_sampler_func=lambda x, gs: cirq.Simulator(), ), 'Syc23-simulator': QuantumProcessor( name='Syc23-simulator', device_obj=cg.Sycamore23, processor_id=None, is_simulator=True, _get_sampler_func=lambda x, gs: cirq.DensityMatrixSimulator( noise=cirq.ConstantQubitNoiseModel( qubit_noise_gate=cirq.DepolarizingChannel(0.005) )) ), 'Syc23-simulator-tester': QuantumProcessor( # This simulator has a constant seed for consistent testing name='Syc23-simulator-tester', device_obj=cg.Sycamore23, processor_id=None, is_simulator=True, _get_sampler_func=lambda x, gs: cirq.DensityMatrixSimulator( noise=cirq.ConstantQubitNoiseModel( qubit_noise_gate=cirq.DepolarizingChannel(0.005) ), seed=1234) ), 'Syc23-zeros': QuantumProcessor( name='Syc23-zeros',
@pytest.mark.parametrize( "gate", [ cirq.BitFlipChannel(0.1), cirq.BitFlipChannel(0.2), cirq.PhaseFlipChannel(0.1), cirq.PhaseFlipChannel(0.2), cirq.PhaseDampingChannel(0.1), cirq.PhaseDampingChannel(0.2), cirq.X.with_probability(0.1), cirq.X.with_probability(0.2), cirq.Y.with_probability(0.1), cirq.Y.with_probability(0.2), cirq.Z.with_probability(0.1), cirq.Z.with_probability(0.2), cirq.DepolarizingChannel(0.1), cirq.DepolarizingChannel(0.2), cirq.DepolarizingChannel(0.1, n_qubits=2), cirq.DepolarizingChannel(0.2, n_qubits=2), ], ) def test_noisy_gate_conversions_compiled_sampler(gate: cirq.Gate): # Create test circuit that uses superdense coding to quantify arbitrary Pauli error mixtures. n = gate.num_qubits() qs = cirq.LineQubit.range(n) circuit = cirq.Circuit( cirq.H.on_each(qs), [cirq.CNOT(q, q + n) for q in qs], gate(*qs), [cirq.CNOT(q, q + n) for q in qs], cirq.H.on_each(qs),
cirq.Circuit( cirq.XPowGate(exponent=sympy.Symbol('theta'), global_shift=0).on(Q0)), # TODO: even the following doesn't work because theta gets # multiplied by 1/pi. # https://github.com/quantumlib/Cirq/issues/2014 # cirq.Circuit(cirq.Rx(sympy.Symbol('theta')).on(Q0)), ], 'ConstantQubitNoiseModel': cirq.ConstantQubitNoiseModel(cirq.X), 'Duration': cirq.Duration(picos=6), 'DensePauliString': cirq.DensePauliString('XYZI', coefficient=1j), 'DepolarizingChannel': cirq.DepolarizingChannel(0.5), 'MutableDensePauliString': cirq.MutableDensePauliString('XXZZ', coefficient=-2), 'FREDKIN': cirq.FREDKIN, 'FSimGate': cirq.FSimGate(theta=0.123, phi=.456), 'Foxtail': cirq.google.Foxtail, 'GateOperation': [ cirq.CCNOT(*cirq.LineQubit.range(3)), cirq.CCZ(*cirq.LineQubit.range(3)), cirq.CNOT(*cirq.LineQubit.range(2)), cirq.CSWAP(*cirq.LineQubit.range(3)), cirq.CZ(*cirq.LineQubit.range(2)) ],
def test_depolarizing_channel_apply_two_qubits(): q0, q1 = cirq.LineQubit.range(2) op = cirq.DepolarizingChannel(p=0.1, n_qubits=2) op(q0, q1)