def bit_flip_circuit(flip0, flip1): q1, q2 = XmonQubit(0, 0), XmonQubit(0, 1) g1, g2 = ExpWGate(half_turns=flip0)(q1), ExpWGate(half_turns=flip1)(q2) m1, m2 = XmonMeasurementGate('q1')(q1), XmonMeasurementGate('q2')(q2) circuit = Circuit() circuit.append([g1, g2, m1, m2]) return circuit
def test_diagram_wgate_none_precision(): qa = cirq.NamedQubit('a') test_wgate = ExpWGate(half_turns=0.12341234, axis_half_turns=0.43214321) c = Circuit([Moment([test_wgate.on(qa)])]) diagram = c.to_text_diagram(use_unicode_characters=False, precision=None) assert diagram.strip() == """ a: ---W(0.43214321)^0.12341234--- """.strip()
def test_handedness_of_xmon_gates(): circuit = Circuit.from_ops( ExpWGate(half_turns=-0.5).on(Q1), ExpZGate(half_turns=-0.5).on(Q1), ExpWGate(axis_half_turns=0.5, half_turns=0.5).on(Q1), XmonMeasurementGate(key='').on(Q1), ) result = xmon_simulator.XmonSimulator().run(circuit) np.testing.assert_equal(result.measurements[''], [[True]])
def simulate(sim_type: str, num_qubits: int, num_gates: int, num_prefix_qubits: int = 0, use_processes: bool = False) -> None: """"Runs the simulator.""" circuit = cirq.Circuit() for _ in range(num_gates): which = np.random.choice(['expz', 'expw', 'exp11']) if which == 'expw': circuit.append(ExpWGate(phase_exponent=np.random.random(), exponent=np.random.random()).on( np.random.randint(num_qubits)), strategy=cirq.InsertStrategy.EARLIEST) elif which == 'expz': circuit.append(cirq.Z( np.random.randint(num_qubits))**np.random.random(), strategy=cirq.InsertStrategy.EARLIEST) elif which == 'exp11': q1, q2 = np.random.choice(num_qubits, 2, replace=False) circuit.append(cirq.CZ(q1, q2)**np.random.random(), strategy=cirq.InsertStrategy.EARLIEST) if sim_type == _XMON: XmonSimulator( XmonOptions(num_shards=2**num_prefix_qubits, use_processes=use_processes)).run(circuit) elif sim_type == _UNITARY: circuit.apply_unitary_effect_to_state(initial_state=0)
def test_param_resolver_exp_w_axis_half_turns(): exp_w = ExpWGate(half_turns=1.0, axis_half_turns=Symbol('a')) circuit = Circuit() circuit.append(exp_w(Q1)) resolver = ParamResolver({'a': 0.5}) result = compute_gate(circuit, resolver) np.testing.assert_almost_equal(result, np.array([[0, -1], [1, 0]]))
def test_param_resolver_exp_w_multiple_params(): exp_w = ExpWGate(half_turns=Symbol('a'), axis_half_turns=Symbol('b')) circuit = Circuit() circuit.append(exp_w(Q1)) resolver = ParamResolver({'a': -0.5, 'b': 0.5}) result = compute_gate(circuit, resolver) amp = 1.0 / math.sqrt(2) np.testing.assert_almost_equal(result, np.array([[amp, amp], [-amp, amp]]))
def test_handedness_of_xmon_exp_x_gate(): circuit = Circuit.from_ops(ExpWGate(half_turns=0.5).on(Q1)) simulator = xmon_simulator.XmonSimulator() result = list(simulator.simulate_moment_steps(circuit))[-1] cirq.testing.assert_allclose_up_to_global_phase(result.state(), np.array([1, -1j]) * np.sqrt(0.5), atol=1e-7)
def test_param_resolver_param_dict(): exp_w = ExpWGate(half_turns=Symbol('a'), axis_half_turns=0.0) circuit = Circuit() circuit.append(exp_w(Q1)) resolver = ParamResolver({'a': 0.5}) simulator = xmon_simulator.XmonSimulator() result = simulator.run(circuit, resolver) assert result.params.param_dict == {'a': 0.5}
def test_validate_scheduled_operation_not_adjacent_exp_11_exp_w(): d = square_device(3, 3, holes=[XmonQubit(1, 1)]) q0 = XmonQubit(0, 0) p1 = XmonQubit(1, 2) p2 = XmonQubit(2, 2) s = Schedule(d, [ ScheduledOperation.op_at_on(ExpWGate().on(q0), Timestamp(), d), ScheduledOperation.op_at_on(Exp11Gate().on(p1, p2), Timestamp(), d), ]) d.validate_schedule(s)
def test_validate_scheduled_operation_adjacent_exp_11_exp_w(): d = square_device(3, 3, holes=[XmonQubit(1, 1)]) q0 = XmonQubit(0, 0) q1 = XmonQubit(1, 0) q2 = XmonQubit(2, 0) s = Schedule(d, [ ScheduledOperation.op_at_on(ExpWGate().on(q0), Timestamp(), d), ScheduledOperation.op_at_on(Exp11Gate().on(q1, q2), Timestamp(), d), ]) with pytest.raises(ValueError): d.validate_schedule(s)
def large_circuit(): np.random.seed(0) qubits = [XmonQubit(i, 0) for i in range(10)] sqrt_x = ExpWGate(half_turns=0.5, axis_half_turns=0.0) cz = Exp11Gate() circuit = Circuit() for _ in range(11): circuit.append( [sqrt_x(qubit) for qubit in qubits if np.random.random() < 0.5]) circuit.append([cz(qubits[i], qubits[i + 1]) for i in range(9)]) circuit.append([XmonMeasurementGate(key='meas')(*qubits)]) return circuit
def basic_circuit(): sqrt_x = ExpWGate(half_turns=-0.5, axis_half_turns=0.0) z = ExpZGate() cz = Exp11Gate() circuit = Circuit() circuit.append( [sqrt_x(Q1), sqrt_x(Q2), cz(Q1, Q2), sqrt_x(Q1), sqrt_x(Q2), z(Q1)]) return circuit
def test_init(): d = square_device(2, 2, holes=[XmonQubit(1, 1)]) ns = Duration(nanos=1) q00 = XmonQubit(0, 0) q01 = XmonQubit(0, 1) q10 = XmonQubit(1, 0) assert d.qubits == {q00, q01, q10} assert d.duration_of(ExpZGate().on(q00)) == 0 * ns assert d.duration_of(ops.measure(q00)) == ns assert d.duration_of(ops.measure(q00, q01)) == ns assert d.duration_of(ExpWGate().on(q00)) == 2 * ns assert d.duration_of(Exp11Gate().on(q00, q01)) == 3 * ns
def test_run_circuit_sweep(): circuit = Circuit.from_ops( ExpWGate(half_turns=Symbol('a')).on(Q1), XmonMeasurementGate('m').on(Q1), ) sweep = Linspace('a', 0, 10, 11) simulator = xmon_simulator.XmonSimulator() for i, result in enumerate( simulator.run_sweep(circuit, sweep, repetitions=1)): assert result.params['a'] == i assert result.measurements['m'] == [i % 2 != 0]
def _latent_space_circuit_gates(aht, ht, zz): """Helper routine for producing sequence of gates for the latent space circuit. Args: ===== aht, ht, zz : numeric Parameters for latent space circuit Returns: ======== state_prep_gates : list List (ordered sequence) of Cirq gates for the latent space circuit """ input_param_W = ExpWGate(half_turns=ht, axis_half_turns=aht) input_param_Z = ExpZGate(half_turns=zz) circuit = [input_param_W(q11), input_param_Z(q11)] return circuit
def param_Winv(i, j): """Returns parametrized inverse of ExpWGate.""" return ExpWGate(half_turns=-i, axis_half_turns=j)
def param_W(i, j): """Returns parametrized ExpWGate.""" return ExpWGate(half_turns=i, axis_half_turns=j)