def main(): # GENERAL SETTINGS num_system_qubits = 3 num_ancillae = 1 num_qubits = num_system_qubits + num_ancillae n_attempts = 20 logging.info('We are going for 20 attempts.') # SET TARGET GATE AND INTERACTIONS target_gate = qutip.fredkin() # interactions = 'all' fredkin_model = make_XX_model(num_qubits) # HYPERPARAMETERS training_dataset_size = 200 test_dataset_size = 100 n_epochs = 300 batch_size = 2 sgd_method = 'adadelta' learning_rate = 1 decay_rate = 0.1 initial_values = 'random' logging.info('Random initial values') # TAKE CARE NOT TO OVERWRITE PREVIOUSLY SAVED FILES prefix = os.path.join(OUTPUT_DIR, OUTPUT_FILES_NAME) ext = '.pickle' files = glob.glob(prefix + '*') pre_idx = 0 if files: pre_idx = max([int(f[len(prefix):-len(ext)]) for f in files]) # STARTING MESSAGES for idx in range(n_attempts): logging.info('Starting training no.{}'.format(str(idx + 1))) model = qn.model.QubitNetworkGateModel( sympy_expr=fredkin_model, initial_values=initial_values, num_system_qubits=num_system_qubits) optimizer = qn.Optimizer.Optimizer( net=model, learning_rate=learning_rate, decay_rate=decay_rate, n_epochs=n_epochs, batch_size=batch_size, target_gate=target_gate, training_dataset_size=training_dataset_size, test_dataset_size=test_dataset_size, sgd_method=sgd_method, headless=True) optimizer.run() newpath = os.path.join( OUTPUT_DIR, OUTPUT_FILES_NAME + str(1 + pre_idx + idx) + '.pickle') optimizer.save_results(newpath) logging.info('Fidelity obtained: {}'.format(model.average_fidelity()))
def main(): # GENERAL SETTINGS num_system_qubits = 3 num_ancillae = 0 num_qubits = num_system_qubits + num_ancillae # the attempts will be equally (when possible) distributed among these # initial values. Each initial value will be used the same amount of # times. attempted_initvalues = [0, 1, 2, 3, 4] num_attempts_per_initvalue = 10 num_attempts = num_attempts_per_initvalue * len(attempted_initvalues) logging.info('We are going for a total of {}' 'attempts.'.format(num_attempts)) logging.info('The following initial values will be used:' '{}.'.format(attempted_initvalues)) # SET TARGET GATE AND INTERACTIONS target_gate = qutip.fredkin() # interactions = 'all' # fredkin_model = make_reduced_fredkin_model(num_qubits) # HYPERPARAMETERS training_dataset_size = 200 test_dataset_size = 100 n_epochs = 100 batch_size = 2 sgd_method = 'momentum' learning_rate = 1 decay_rate = 0.1 # TAKE CARE NOT TO OVERWRITE PREVIOUSLY SAVED FILES prefix = os.path.join(OUTPUT_DIR, OUTPUT_FILES_NAME) ext = '.pickle' files = glob.glob(prefix + '*') pre_idx = 0 if files: pre_idx = max([int(f[len(prefix):-len(ext)]) for f in files]) # STARTING MESSAGES for idx in range(num_attempts): initial_values = attempted_initvalues[idx // num_attempts_per_initvalue] fredkin_model = make_reduced_fredkin_model(num_qubits) logging.info('Starting training no.{}'.format(str(idx + 1))) logging.info('Initial values: {}.'.format(initial_values)) model = qn.model.QubitNetworkGateModel( sympy_expr=fredkin_model, initial_values=initial_values, num_system_qubits=num_system_qubits) optimizer = qn.Optimizer.Optimizer( net=model, learning_rate=learning_rate, decay_rate=decay_rate, n_epochs=n_epochs, batch_size=batch_size, target_gate=target_gate, training_dataset_size=training_dataset_size, test_dataset_size=test_dataset_size, sgd_method=sgd_method, headless=True) optimizer.run() newpath = os.path.join( OUTPUT_DIR, OUTPUT_FILES_NAME + str(1 + pre_idx + idx) + '.pickle') optimizer.save_results(newpath) logging.info('Fidelity obtained: {}'.format(model.average_fidelity()))
import qutip as qt import numpy as np """ fredkin hadamard ancilla---------O---------H------------Measure f1---------X---------------------- f2---------X---------------------- """ up = qt.qstate('u') #qstate down is (1,0), qstate up is (0,1) down = qt.qstate('d') ancilla = 1 / np.sqrt(2) * (up + down) hadamard = qt.tensor(qt.snot(), qt.qeye(2), qt.qeye(2)) fredkin = qt.fredkin() #fredkin swapes f1 and f2 if ancilla = 1 #------------------------------------------------------------------------ #Define Input f1 = 1 / np.sqrt(2) * (up + down) f2 = up #------------------------------------------------------------------------ psi0 = qt.tensor(ancilla, f1, f2) output = hadamard * fredkin * psi0 measure = qt.tensor(down, f1, f2).dag() * output #if f1 = f2 the output is (down,f1,f2) #therefore measure is 1 if f1 = f2 #measure is 0.5 if f1, f2 are orthogonal print(measure)
def make_reduced_fredkin_model(num_qubits): import qubit_network.analytical_conditions as ac fredkin_diagonal = ac.commuting_generator(qutip.fredkin(), interactions='diagonal') return fredkin_diagonal
return qt.phasegate(angle) # 2 qubit gates CNOT = qt.cnot() CZ = qt.csign() Berkeley = qt.berkeley() SWAP = qt.swap() iSWAP = qt.iswap() SQSWAP = qt.sqrtswap() SQiSWAP = qt.sqrtiswap() def aSWAP(angle): return qt.swapalpha(angle) # 3 qubit gates Fredkin = qt.fredkin() Toffoli = qt.toffoli() # Get unitary axis and angle def bloch(U): if isinstance(U, qt.Qobj): U = U.full() angle = np.real(2 * np.arccos(np.trace(U) / 2)) sin = np.sin(angle / 2) eps = 1e-10 if sin < eps: axis = [0, 0, 1] else: nz = np.imag(U[1, 1] - U[0, 0]) / (2 * sin)