def test_parameterizable(resolve_fn): a = sympy.Symbol('a') cy = cirq.ControlledGate(cirq.Y) cya = cirq.ControlledGate(cirq.YPowGate(exponent=a)) assert cirq.is_parameterized(cya) assert not cirq.is_parameterized(cy) assert resolve_fn(cya, cirq.ParamResolver({'a': 1})) == cy cchan = cirq.ControlledGate( cirq.RandomGateChannel(sub_gate=cirq.PhaseDampingChannel(0.1), probability=a)) with pytest.raises(ValueError, match='Cannot control channel'): resolve_fn(cchan, cirq.ParamResolver({'a': 0.1}))
def test_controlled_operation_init(): class G(cirq.testing.SingleQubitGate): def _has_mixture_(self): return True g = G() cb = cirq.NamedQubit('ctr') q = cirq.NamedQubit('q') v = cirq.GateOperation(g, (q, )) c = cirq.ControlledOperation([cb], v) assert c.sub_operation == v assert c.controls == (cb, ) assert c.qubits == (cb, q) assert c == c.with_qubits(cb, q) assert c.control_values == ((1, ), ) assert cirq.qid_shape(c) == (2, 2) c = cirq.ControlledOperation([cb], v, control_values=[0]) assert c.sub_operation == v assert c.controls == (cb, ) assert c.qubits == (cb, q) assert c == c.with_qubits(cb, q) assert c.control_values == ((0, ), ) assert cirq.qid_shape(c) == (2, 2) c = cirq.ControlledOperation([cb.with_dimension(3)], v) assert c.sub_operation == v assert c.controls == (cb.with_dimension(3), ) assert c.qubits == (cb.with_dimension(3), q) assert c == c.with_qubits(cb.with_dimension(3), q) assert c.control_values == ((1, ), ) assert cirq.qid_shape(c) == (3, 2) with pytest.raises(ValueError, match=r'len\(control_values\) != len\(controls\)'): _ = cirq.ControlledOperation([cb], v, control_values=[1, 1]) with pytest.raises(ValueError, match='Control values .*outside of range'): _ = cirq.ControlledOperation([cb], v, control_values=[2]) with pytest.raises(ValueError, match='Control values .*outside of range'): _ = cirq.ControlledOperation([cb], v, control_values=[(1, -1)]) with pytest.raises(ValueError, match=re.escape("Duplicate control qubits ['ctr'].")): _ = cirq.ControlledOperation([cb, cirq.LineQubit(0), cb], cirq.X(q)) with pytest.raises( ValueError, match=re.escape("Sub-op and controls share qubits ['ctr']")): _ = cirq.ControlledOperation([cb, cirq.LineQubit(0)], cirq.CX(cb, q)) with pytest.raises(ValueError, match='Cannot control measurement'): _ = cirq.ControlledOperation([cb], cirq.measure(q)) with pytest.raises(ValueError, match='Cannot control channel'): _ = cirq.ControlledOperation([cb], cirq.PhaseDampingChannel(1)(q))
def test_parameterizable(resolve_fn): a = sympy.Symbol('a') qubits = cirq.LineQubit.range(3) cz = cirq.ControlledOperation(qubits[:1], cirq.Z(qubits[1])) cza = cirq.ControlledOperation(qubits[:1], cirq.ZPowGate(exponent=a)(qubits[1])) assert cirq.is_parameterized(cza) assert not cirq.is_parameterized(cz) assert resolve_fn(cza, cirq.ParamResolver({'a': 1})) == cz cchan = cirq.ControlledOperation( [qubits[0]], cirq.RandomGateChannel(sub_gate=cirq.PhaseDampingChannel(0.1), probability=a)(qubits[1]), ) with pytest.raises(ValueError, match='Cannot control channel'): resolve_fn(cchan, cirq.ParamResolver({'a': 0.1}))
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 channel_mapping[cirq.GeneralizedAmplitudeDampingChannel(0.01, 0.02)] = 1 channel_mapping[cirq.AmplitudeDampingChannel(0.01)] = 1 channel_mapping[cirq.ResetChannel()] = 1 channel_mapping[cirq.PhaseDampingChannel(0.01)] = 1 channel_mapping[cirq.PhaseFlipChannel(0.01)] = 1 channel_mapping[cirq.BitFlipChannel(0.01)] = 1 return channel_mapping
def test_phase_damping_channel_repr(): cirq.testing.assert_equivalent_repr(cirq.PhaseDampingChannel(0.3))
cirq.Z(Q2)]), ], 'NO_NOISE': cirq.NO_NOISE, 'NamedQubit': cirq.NamedQubit('hi mom'), 'PauliString': [ cirq.PauliString({ Q0: cirq.X, Q1: cirq.Y, Q2: cirq.Z }), cirq.X(Q0) * cirq.Y(Q1) * 123 ], 'PhaseDampingChannel': cirq.PhaseDampingChannel(0.5), 'PhaseFlipChannel': cirq.PhaseFlipChannel(0.5), 'PhaseGradientGate': cirq.PhaseGradientGate(num_qubits=3, exponent=0.235), 'PhasedISwapPowGate': cirq.PhasedISwapPowGate(phase_exponent=0.1, exponent=0.2), 'PhasedXPowGate': cirq.PhasedXPowGate(phase_exponent=0.123, exponent=0.456, global_shift=0.789), 'QuantumFourierTransformGate': cirq.QuantumFourierTransformGate(num_qubits=2, without_reverse=True), 'ResetChannel': cirq.ResetChannel(), 'X':
def test_init2(): with pytest.raises(ValueError, match=r'len\(control_values\) != num_controls'): cirq.ControlledGate(cirq.Z, num_controls=1, control_values=(1, 0)) with pytest.raises(ValueError, match=r'len\(control_qid_shape\) != num_controls'): cirq.ControlledGate(cirq.Z, num_controls=1, control_qid_shape=(2, 2)) with pytest.raises(ValueError, match='Control values .*outside of range'): cirq.ControlledGate(cirq.Z, control_values=[2]) with pytest.raises(ValueError, match='Control values .*outside of range'): cirq.ControlledGate(cirq.Z, control_values=[(1, -1)]) with pytest.raises(ValueError, match='Control values .*outside of range'): cirq.ControlledGate(cirq.Z, control_values=[3], control_qid_shape=[3]) with pytest.raises(ValueError, match='Cannot control measurement'): cirq.ControlledGate(cirq.MeasurementGate(1)) with pytest.raises(ValueError, match='Cannot control channel'): cirq.ControlledGate(cirq.PhaseDampingChannel(1)) gate = cirq.ControlledGate(cirq.Z, 1) assert gate.sub_gate is cirq.Z assert gate.num_controls() == 1 assert gate.control_values == ((1, ), ) assert gate.control_qid_shape == (2, ) assert gate.num_qubits() == 2 assert cirq.qid_shape(gate) == (2, 2) gate = cirq.ControlledGate(cirq.Z, 2) assert gate.sub_gate is cirq.Z assert gate.num_controls() == 2 assert gate.control_values == ((1, ), (1, )) assert gate.control_qid_shape == (2, 2) assert gate.num_qubits() == 3 assert cirq.qid_shape(gate) == (2, 2, 2) gate = cirq.ControlledGate( cirq.ControlledGate(cirq.ControlledGate(cirq.Z, 3), num_controls=2), 2) assert gate.sub_gate is cirq.Z assert gate.num_controls() == 7 assert gate.control_values == ((1, ), ) * 7 assert gate.control_qid_shape == (2, ) * 7 assert gate.num_qubits() == 8 assert cirq.qid_shape(gate) == (2, ) * 8 op = gate(*cirq.LineQubit.range(8)) assert op.qubits == ( cirq.LineQubit(0), cirq.LineQubit(1), cirq.LineQubit(2), cirq.LineQubit(3), cirq.LineQubit(4), cirq.LineQubit(5), cirq.LineQubit(6), cirq.LineQubit(7), ) gate = cirq.ControlledGate(cirq.Z, control_values=(0, (0, 1))) assert gate.sub_gate is cirq.Z assert gate.num_controls() == 2 assert gate.control_values == ((0, ), (0, 1)) assert gate.control_qid_shape == (2, 2) assert gate.num_qubits() == 3 assert cirq.qid_shape(gate) == (2, 2, 2) gate = cirq.ControlledGate(cirq.Z, control_qid_shape=(3, 3)) assert gate.sub_gate is cirq.Z assert gate.num_controls() == 2 assert gate.control_values == ((1, ), (1, )) assert gate.control_qid_shape == (3, 3) assert gate.num_qubits() == 3 assert cirq.qid_shape(gate) == (3, 3, 2)
assert (str(c).strip() == """ H 0 CX 0 1 M 0 1 R 0 """.strip()) @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.