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(device=test_device)

    for _ in range(num_gates):
        which = np.random.choice(['expz', 'expw', 'exp11'])
        if which == 'expw':
            q1 = cirq.GridQubit(0, np.random.randint(num_qubits))
            circuit.append(
                cirq.PhasedXPowGate(
                    phase_exponent=np.random.random(),
                    exponent=np.random.random()
                ).on(q1))
        elif which == 'expz':
            q1 = cirq.GridQubit(0, np.random.randint(num_qubits))
            circuit.append(
                cirq.Z(q1)**np.random.random())
        elif which == 'exp11':
            q1 = cirq.GridQubit(0, np.random.randint(num_qubits - 1))
            q2 = cirq.GridQubit(0, q1.col + 1)
            circuit.append(cirq.CZ(q1, q2)**np.random.random())

    if sim_type == _XMON:
        options = cg.XmonOptions(num_shards=2 ** num_prefix_qubits,
                                 use_processes=use_processes)
        cg.XmonSimulator(options).run(circuit)
    elif sim_type == _UNITARY:
        circuit.apply_unitary_effect_to_state(initial_state=0)
    elif sim_type == _DENSITY:
        cirq.DensityMatrixSimulator().run(circuit)
def simulate ( sim_type : str , num_qubits : int , num_gates : int , num_prefix_qubits : int = 0 , use_processes : bool = False ) -> None : circuit = cirq . Circuit ( device = test_device ) for _ in range ( num_gates ) : which = np . random . choice ( [ 'expz' , 'expw' , 'exp11' ] ) if which == 'expw' : q1 = cirq . GridQubit ( 0 , np . random . randint ( num_qubits ) ) circuit . append ( cirq . PhasedXPowGate ( phase_exponent = np . random . random ( ) , exponent = np . random . random ( ) ) . on ( q1 ) ) elif which == 'expz' : q1 = cirq . GridQubit ( 0 , np . random . randint ( num_qubits ) ) circuit . append ( cirq . Z ( q1 ) ** np . random . random ( ) ) elif which == 'exp11' : q1 = cirq . GridQubit ( 0 , np . random . randint ( num_qubits - 1 ) ) q2 = cirq . GridQubit ( 0 , q1 . col + 1 ) circuit . append ( cirq . CZ ( q1 , q2 ) ** np . random . random ( ) ) if sim_type == _XMON : options = cg . XmonOptions ( num_shards = 2 ** num_prefix_qubits , use_processes = use_processes ) cg . XmonSimulator ( options ) . run ( circuit ) elif sim_type == _UNITARY : circuit . apply_unitary_effect_to_state ( initial_state = 0 ) elif sim_type == _DENSITY : cirq . DensityMatrixSimulator ( ) . run ( circuit )
Runs the simulator.