def test_validate_operation_existing_qubits(): d = square_device(3, 3, holes=[XmonQubit(1, 1)]) d.validate_operation( ops.Operation(Exp11Gate(), (XmonQubit(0, 0), XmonQubit(1, 0)))) d.validate_operation(ops.Operation(ExpZGate(), (XmonQubit(0, 0), ))) with pytest.raises(ValueError): d.validate_operation( ops.Operation(Exp11Gate(), (XmonQubit(0, 0), XmonQubit(-1, 0)))) with pytest.raises(ValueError): d.validate_operation(ops.Operation(ExpZGate(), (XmonQubit(-1, 0), ))) with pytest.raises(ValueError): d.validate_operation( ops.Operation(Exp11Gate(), (XmonQubit(1, 0), XmonQubit(1, 1))))
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 = Circuit() for _ in range(num_gates): which = np.random.choice(['expz', 'expw', 'exp11']) if which == 'expw': circuit.append(ExpWGate(axis_half_turns=np.random.random(), half_turns=np.random.random()).on( np.random.randint(num_qubits)), strategy=InsertStrategy.EARLIEST) elif which == 'expz': circuit.append(ExpZGate(half_turns=np.random.random()).on( np.random.randint(num_qubits)), strategy=InsertStrategy.EARLIEST) elif which == 'exp11': q1, q2 = np.random.choice(num_qubits, 2, replace=False) circuit.append(Exp11Gate(half_turns=np.random.random()).on(q1, q2), strategy=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_ignores_czs_separated_by_parameterized(): q0 = ops.QubitId() q1 = ops.QubitId() assert_optimizes( before=circuits.Circuit([ circuits.Moment([ops.CZ(q0, q1)]), circuits.Moment([ExpZGate( half_turns=Symbol('boo'))(q0)]), circuits.Moment([ops.CZ(q0, q1)]), ]), after=circuits.Circuit([ circuits.Moment([ops.CZ(q0, q1)]), circuits.Moment([ExpZGate( half_turns=Symbol('boo'))(q0)]), circuits.Moment([ops.CZ(q0, q1)]), ]))
def test_handedness_of_xmon_exp_z_gate(): circuit = Circuit.from_ops(H(Q1), ExpZGate(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_validate_operation_supported_gate(): d = square_device(3, 3) class MyGate(ops.Gate): pass d.validate_operation(ops.Operation(ExpZGate(), [XmonQubit(0, 0)])) with pytest.raises(ValueError): d.validate_operation(ops.Operation(MyGate, [XmonQubit(0, 0)]))
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 test_validate_scheduled_operation_adjacent_exp_11_exp_z(): 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(ExpZGate().on(q0), Timestamp(), d), ScheduledOperation.op_at_on(Exp11Gate().on(q1, q2), Timestamp(), d), ]) d.validate_schedule(s)
def test_param_resolver_exp_z_half_turns(): exp_z = ExpZGate(half_turns=Symbol('a')) circuit = Circuit() circuit.append(exp_z(Q1)) resolver = ParamResolver({'a': -0.5}) result = compute_gate(circuit, resolver) np.testing.assert_almost_equal( result, np.array([[cmath.exp(1j * math.pi * 0.25), 0], [0, cmath.exp(-1j * math.pi * 0.25)]]))
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 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 _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_Z(i): """Returns parametrized ExpZGate.""" return ExpZGate(half_turns=i)