Skip to content

Commit

Permalink
A really big commit
Browse files Browse the repository at this point in the history
  • Loading branch information
taiypeo committed Jul 31, 2017
1 parent a4138a7 commit d821015
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 42 deletions.
153 changes: 133 additions & 20 deletions com/qwertygid/deutschsim/GUI/CustomGatePrompt.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
Expand All @@ -21,6 +24,11 @@
import javax.swing.event.ChangeListener;
import javax.swing.text.JTextComponent;

import org.apache.commons.math3.complex.Complex;

import com.qwertygid.deutschsim.Logic.Gate;
import com.qwertygid.deutschsim.Math.Interpreter;
import com.qwertygid.deutschsim.Math.LexicalAnalyzer;
import com.qwertygid.deutschsim.Miscellaneous.Tools;

public class CustomGatePrompt extends JDialog {
Expand All @@ -34,17 +42,26 @@ public CustomGatePrompt(final JFrame frame) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

JTabbedPane tabbed_pane = new JTabbedPane();
tabbed_pane.add("Rotation", create_rotation_panel());
tabbed_pane.add("Phase Shift", create_phase_shift_panel());
tabbed_pane.add("Matrix", create_matrix_panel());
gate_name = new TextEditor("Gate name", TextEditor.Type.SINGLE_LINE);
panel.add(gate_name);

tabbed_pane = new JTabbedPane();

create_rotation_panel();
tabbed_pane.add("Rotation", rotation_panel);

create_phase_shift_panel();
tabbed_pane.add("Phase Shift", phase_shift_panel);

create_matrix_panel();
tabbed_pane.add("Matrix", matrix_panel);

tabbed_pane.addChangeListener(new TabbedPaneListener(tabbed_pane));
panel.add(tabbed_pane);

matrix_selection = new AngleTypeSelection("What to represent arguments of " +
trig_selection = new AngleTypeSelection("What to represent arguments of " +
"trigonometric functions in?");
panel.add(matrix_selection);
panel.add(trig_selection);

option_pane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_OPTION,
null, new Object[] {"Create Gate"});
Expand All @@ -54,15 +71,19 @@ public CustomGatePrompt(final JFrame frame) {
setContentPane(option_pane);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();

option_pane.addPropertyChangeListener(new OKButtonListener());
}

public void show_prompt() {
public Gate show_prompt() {
setLocationRelativeTo(frame);
setVisible(true);

return result_gate;
}

private JPanel create_rotation_panel() {
JPanel rotation_panel = new JPanel();
private void create_rotation_panel() {
rotation_panel = new JPanel();
rotation_panel.setLayout(new BorderLayout());

JPanel text_editor_panel = new JPanel();
Expand All @@ -81,25 +102,21 @@ private JPanel create_rotation_panel() {

rot_selection = new AngleTypeSelection("What to represent angles in?");
rotation_panel.add(rot_selection, BorderLayout.SOUTH);

return rotation_panel;
}

private JPanel create_phase_shift_panel() {
JPanel phase_shift_panel = new JPanel();
private void create_phase_shift_panel() {
phase_shift_panel = new JPanel();
phase_shift_panel.setLayout(new BorderLayout());

phase = new TextEditor("Phase", TextEditor.Type.SINGLE_LINE);
phase_shift_panel.add(phase, BorderLayout.NORTH);

phase_selection = new AngleTypeSelection("What to represent phase in?");
phase_shift_panel.add(phase_selection, BorderLayout.SOUTH);

return phase_shift_panel;
}

private JPanel create_matrix_panel() {
JPanel matrix_panel = new JPanel();
private void create_matrix_panel() {
matrix_panel = new JPanel();
matrix_panel.setLayout(new BorderLayout());

JScrollPane scroll_pane = new JScrollPane();
Expand All @@ -109,21 +126,29 @@ private JPanel create_matrix_panel() {
scroll_pane.setViewportView(matrix);

matrix_panel.add(scroll_pane, BorderLayout.CENTER);

return matrix_panel;
}

private final JFrame frame;
private final JOptionPane option_pane;

private TextEditor gate_name;

private JTabbedPane tabbed_pane;

private JPanel rotation_panel;
private TextEditor x_rot, y_rot, z_rot;
private AngleTypeSelection rot_selection;

private JPanel phase_shift_panel;
private TextEditor phase;
private AngleTypeSelection phase_selection;

private JPanel matrix_panel;
private TextEditor matrix;
private AngleTypeSelection matrix_selection;

private AngleTypeSelection trig_selection;

private Gate result_gate;

private static class TextEditor extends JPanel {
private static final long serialVersionUID = 6799884214616868976L;
Expand Down Expand Up @@ -225,4 +250,92 @@ public void stateChanged(ChangeEvent ev) {

private final JTabbedPane tabbed_pane;
}

private class OKButtonListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent ev) {
final String prop = ev.getPropertyName();
if (isVisible()
&& ev.getSource() == option_pane
&& (JOptionPane.VALUE_PROPERTY.equals(prop)
|| JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
if (option_pane.getValue() == JOptionPane.UNINITIALIZED_VALUE)
return;
option_pane.setValue(JOptionPane.UNINITIALIZED_VALUE);

final Interpreter interpreter = new Interpreter(trig_selection.get_selected_type());

final Component selected_panel = tabbed_pane.getSelectedComponent();
if (selected_panel == rotation_panel)
create_rotation_gate(interpreter);
else if (selected_panel == phase_shift_panel) {

} else if (selected_panel == matrix_panel) {

} else
throw new RuntimeException("Selected tab is not listed in the tabbed panel");
}
}

private void create_rotation_gate(final Interpreter interpreter) {
Complex x_rot_complex, y_rot_complex, z_rot_complex;

String x_rot_text = x_rot.get_text(), y_rot_text = y_rot.get_text(),
z_rot_text = z_rot.get_text();
if (x_rot_text.equals("X axis rotation angle"))
x_rot_text = "0";
if (y_rot_text.equals("Y axis rotation angle"))
y_rot_text = "0";
if (z_rot_text.equals("Z axis rotation angle"))
z_rot_text = "0";

try {
interpreter.set_lexical_analyzer(new LexicalAnalyzer(x_rot_text));
x_rot_complex = interpreter.interpret();

interpreter.set_lexical_analyzer(new LexicalAnalyzer(y_rot_text));
y_rot_complex = interpreter.interpret();

interpreter.set_lexical_analyzer(new LexicalAnalyzer(z_rot_text));
z_rot_complex = interpreter.interpret();
} catch (RuntimeException ex) {
Tools.error(frame, "A runtime exception has been caught:\n" +
ex.getMessage());
return;
}

if (x_rot_complex.equals(y_rot_complex)
&& y_rot_complex.equals(z_rot_complex)
&& z_rot_complex.equals(Complex.ZERO)) {
dispose();
return;
}

if (!Tools.equal(x_rot_complex.getImaginary(), 0.0)
|| !Tools.equal(y_rot_complex.getImaginary(), 0.0)
|| !Tools.equal(z_rot_complex.getImaginary(), 0.0)) {
Tools.error(frame, "Cannot use complex numbers as rotation angles");
return;
}

String name = gate_name.get_text();
if (name.equals("Gate name"))
name = "G";

final double x_rot_angle = Tools.round_if_needed(x_rot_complex.getReal()),
y_rot_angle = Tools.round_if_needed(y_rot_complex.getReal()),
z_rot_angle = Tools.round_if_needed(z_rot_complex.getReal());

try {
result_gate = new Gate(name, x_rot_angle, y_rot_angle,
z_rot_angle, rot_selection.get_selected_type());
} catch (RuntimeException ex) {
Tools.error(frame, "A runtime exception has been caught:\n" +
ex.getMessage());
return;
}

dispose();
}
}
}
7 changes: 4 additions & 3 deletions com/qwertygid/deutschsim/GUI/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ public void actionPerformed(ActionEvent arg0) {
continue;

double current_percentage = Math.pow(current_magnitude, 2) * 100;
if (Tools.equal(current_percentage, Math.round(current_percentage)))
current_percentage = Math.round(current_percentage);
current_percentage = Tools.round_if_needed(current_percentage);

StringBuilder qubits_values = new StringBuilder(Integer.toBinaryString(index));
for (int length = qubits_values.length(); length < qubits_number; length++)
Expand Down Expand Up @@ -387,7 +386,9 @@ private void add_item_create_custom_gate(final JMenu circuit_menu) {
@Override
public void actionPerformed(ActionEvent arg0) {
CustomGatePrompt prompt = new CustomGatePrompt(frame);
prompt.show_prompt();
Gate gate = prompt.show_prompt();
if (gate != null)
((DefaultListModel<Gate>) gate_list.getModel()).addElement(gate);
}
});
item_create_custom_gate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 0));
Expand Down
22 changes: 11 additions & 11 deletions com/qwertygid/deutschsim/Logic/Gate.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@

public class Gate {
// Rotation constructor
public Gate(final String id, double angle_x, double angle_y, double angle_z, final boolean in_degrees)
public Gate(final String id, double angle_x, double angle_y, double angle_z, final Tools.AngleType angle_type)
{
this.id = id;
this.IO_ports = 1;

if (in_degrees) {
if (angle_type == Tools.AngleType.DEGREES) {
angle_x = Math.toRadians(angle_x);
angle_y = Math.toRadians(angle_y);
angle_z = Math.toRadians(angle_z);
}

Complex[][] data_x_rot = new Complex[][] {
{new Complex(Math.cos(angle_x / 2)), new Complex(0.0, -Math.sin(angle_x / 2))},
{new Complex(0.0, -Math.sin(angle_x / 2)), new Complex(Math.cos(angle_x / 2))}
{new Complex(Tools.round_if_needed(Math.cos(angle_x / 2))), new Complex(0.0, Tools.round_if_needed(-Math.sin(angle_x / 2)))},
{new Complex(0.0, Tools.round_if_needed(-Math.sin(angle_x / 2))), new Complex(Tools.round_if_needed(Math.cos(angle_x / 2)))}
};
FieldMatrix<Complex> x_rot = new Array2DRowFieldMatrix<Complex>(data_x_rot);

Complex[][] data_y_rot = new Complex[][] {
{new Complex(Math.cos(angle_y / 2)), new Complex(-Math.sin(angle_y / 2))},
{new Complex(Math.sin(angle_y / 2)), new Complex(Math.cos(angle_y / 2))}
{new Complex(Tools.round_if_needed(Math.cos(angle_y / 2))), new Complex(Tools.round_if_needed(-Math.sin(angle_y / 2)))},
{new Complex(Tools.round_if_needed(Math.sin(angle_y / 2))), new Complex(Tools.round_if_needed(Math.cos(angle_y / 2)))}
};
FieldMatrix<Complex> y_rot = new Array2DRowFieldMatrix<Complex>(data_y_rot);

Complex[][] data_z_rot = new Complex[][] {
{new Complex(Math.cos(angle_z / 2), -Math.sin(angle_z / 2)), new Complex(0.0)},
{new Complex(0.0), new Complex(Math.cos(angle_z / 2), Math.sin(angle_z / 2))}
{new Complex(Tools.round_if_needed(Math.cos(angle_z / 2)), Tools.round_if_needed(-Math.sin(angle_z / 2))), new Complex(0.0)},
{new Complex(0.0), new Complex(Tools.round_if_needed(Math.cos(angle_z / 2)), Tools.round_if_needed(Math.sin(angle_z / 2)))}
};
FieldMatrix<Complex> z_rot = new Array2DRowFieldMatrix<Complex>(data_z_rot);

Expand All @@ -47,16 +47,16 @@ public Gate(final String id, double angle_x, double angle_y, double angle_z, fin
}

// Phase shift constructor
public Gate(final String id, double angle, final boolean in_degrees) {
public Gate(final String id, double angle, final Tools.AngleType angle_type) {
this.id = id;
this.IO_ports = 1;

if (in_degrees)
if (angle_type == Tools.AngleType.DEGREES)
angle = Math.toRadians(angle);

Complex[][] data = new Complex[][] {
{new Complex(1), new Complex(0)},
{new Complex(0), new Complex(Math.cos(angle), Math.sin(angle))}
{new Complex(0), new Complex(Tools.round_if_needed(Math.cos(angle)), Tools.round_if_needed(Math.sin(angle)))}
};

mat = new Array2DRowFieldMatrix<Complex>(data);
Expand Down
16 changes: 8 additions & 8 deletions com/qwertygid/deutschsim/Math/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import com.qwertygid.deutschsim.Miscellaneous.Tools;

public class Interpreter {
public Interpreter(final LexicalAnalyzer analyzer, final Tools.AngleType angle_type) {
public Interpreter(final Tools.AngleType angle_type) {
this.angle_type = angle_type;
}

public void set_lexical_analyzer(final LexicalAnalyzer analyzer) {
this.analyzer = analyzer;
current = analyzer.get_next_token();

this.angle_type = angle_type;
}

public Complex interpret() {
Expand All @@ -21,10 +23,8 @@ public Complex interpret() {

double real = value.getReal(), imag = value.getImaginary();

if (Tools.equal(real, Math.round(real)))
real = Math.round(real);
if (Tools.equal(imag, Math.round(imag)))
imag = Math.round(imag);
real = Tools.round_if_needed(real);
imag = Tools.round_if_needed(imag);

return new Complex(real, imag);
}
Expand Down Expand Up @@ -202,7 +202,7 @@ private Complex convert_to_radians(final Complex angle) {
return angle;
}

private final LexicalAnalyzer analyzer;
private final Tools.AngleType angle_type;
private LexicalAnalyzer analyzer;
private Token current;
}
7 changes: 7 additions & 0 deletions com/qwertygid/deutschsim/Miscellaneous/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public static boolean equal(final double a, final double b) {
return Math.abs(a - b) < EPSILON;
}

public static double round_if_needed(final double a) {
if (Tools.equal(a, Math.round(a)))
return Math.round(a);

return a;
}

public static void error(final JFrame frame, final String msg) {
JOptionPane.showMessageDialog(frame, msg,
"Error", JOptionPane.ERROR_MESSAGE);
Expand Down

0 comments on commit d821015

Please sign in to comment.